1.
package com.Jasper2003.servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet("/demo_do") public class DemoServlet extends HttpServlet{ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.getWriter().append("I'm doGet method!"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.getWriter().append("I'm doPost method!"); } }
2. JSP更加适合做展示, Servlet更加适合做处理.
反例: 当用户访问demo_do时, Servlet返回给用户一个网页
@WebServlet("/demo_do") public class DemoServlet extends HttpServlet{ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.getWriter().append(""); resp.getWriter().append(""); resp.getWriter().append(""); resp.getWriter().append(""); resp.getWriter().append(""); resp.getWriter().append(""); resp.getWriter().append("Hi! doGet method!"); resp.getWriter().append(""); resp.getWriter().append(""); }
效果:
可见, 这是非常麻烦的
正例: 利用Servlet处理注册请求
1. 利用RegisterServlet.java处理注册请求:
<1> 在RegisterServlet.java中:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); String password = request.getParameter("password"); int age = Integer.parseInt(request.getParameter("age")); String gender = request.getParameter("gender"); gender = new String(gender.getBytes("iso-8859-1"), "utf-8"); boolean isSuccess = DBUtil.addUser(username, password, age, gender); if(isSuccess){ request.setAttribute("message","你注册成功了,请登录!"); request.getRequestDispatcher("login.jsp").forward(request,response); }else{ request.setAttribute("message","很遗憾, 由于用户名重复, 注册失败了, 请你重新注册!"); request.getRequestDispatcher("register.jsp").forward(request,response); } }
<2> 在register.jsp中 (将表单提交的地址修改为从register_do.jsp修改为register_do):
效果:
正例2: 利用Servlet 完成登录请求
<1> 在LoginServlet中:
@WebServlet("/login_do")
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
User user = DBUtil.verifyAccount(username,password);
if(user == null){
request.setAttribute("message","很遗憾,由于用户名或密码错误, 你登录失败了!");
request.getRequestDispatcher("login.jsp").forward(request,response);
}else{
int number = 0;
ServletContext application = this.getServletContext();
if(application.getAttribute("userNumber") != null){
number = (Integer)application.getAttribute("userNumber");
}
number++;
application.setAttribute("userNumber",number);
request.getSession().setAttribute("user",user);
request.getRequestDispatcher("personalCenter.jsp").forward(request,response);
}
}
<2> 在login.jsp中:
效果: