servlet获取表单数据

login.jsp关键代码


    
用户名:
密码:

userServlet关键代码

public class userServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public userServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");//处理表单中文乱码
		user u=new user();
		String username,password;
		try{
			username=request.getParameter("username");
			password=request.getParameter("password");
			u.setUsername(username);
			u.setPassword(password);
			request.getSession().setAttribute("use", u);
			request.getRequestDispatcher("/Success.jsp").forward(request, response);
		}catch(Exception e){
			e.printStackTrace();
		}
	}

}

Success.jsp关键代码


    
用户名:
密码:

你可能感兴趣的:(java,web)