首页 > 代码库 > Servlet笔记11--补充
Servlet笔记11--补充
Servlet线程安全问题:
代码示例:
1 package com.bjpowernode.javaweb.servlet; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 6 import javax.servlet.ServletException; 7 import javax.servlet.http.HttpServlet; 8 import javax.servlet.http.HttpServletRequest; 9 import javax.servlet.http.HttpServletResponse; 10 11 public class RegisterServlet extends HttpServlet { 12 13 private static final long serialVersionUID = 1L; 14 15 //实例变量 16 private String username; 17 18 @Override 19 protected void doPost(HttpServletRequest request, HttpServletResponse response) 20 throws ServletException, IOException { 21 22 response.setContentType("text/html;charset=UTF-8"); 23 PrintWriter out = response.getWriter(); 24 request.setCharacterEncoding("UTF-8"); 25 26 //局部变量 27 //String username = request.getParameter("username"); 28 29 synchronized(this){ 30 //实例变量 31 username = request.getParameter("username"); 32 out.print("恭喜你" + username + "注册成功"); 33 } 34 } 35 36 37 }
路径编写总结:
Servlet笔记11--补充
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。