Servlet笔记11--补充
Posted 拉夫德尔
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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--补充的主要内容,如果未能解决你的问题,请参考以下文章