1.登录验证
用户输入用户名密码登录,提交到CheckUserServlet,正确则径入欢迎页面,错误再回到登录页面
login.html
<form action="/checkUserServlet" method="post">
用户名:<input type="text" name="username"><br>
密码:<input type="password" name="pass"><br>
<input type="submit" value="登录">
</form>
进行注解
public class CheckUserServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String user=request.getParameter("username");//获得请求参数:username
String pass=request.getParameter("pass");
if(user.equals("admin")&&pass.equals("1234")){
request.setAttribute("msg","欢迎你登陆成功");//request存储数据
request.getRequestDispatcher("welcomeServlet").forward(request,response);//请求转发到welcomeServlet
}else{
response.sendRedirect("login.html");//重定向到login.html
}
}
}
WelcomeServlet
Java
@WebServlet(value = "/welcomeServlet")
public class WelcomeServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request,response);//调用doGet这样两种请求都可以截获
}
welcome类
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String msg = (String) request.getAttribute("msg");//获得CheckUserServlet存储的数据
response.setContentType("text/html;charset=utf-8");//设置响应的内容类型
PrintWriter out=response.getWriter();//获得响应的输出流
out.println("<h1>"+msg+"</h1>");
out.close();
}
2.添加和查询
先建立jsp。
<form action="/addDept" method="post">
部门名称:<input type="text" name="dname"><br>
创建日期:<input type="date" name="createTime"><br>
<input type="submit" value="添加">
</form>
添加:
WebServlet(value = "/addDept")
public class AddDeptServlet extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String user=request.getParameter("dname");
String date=request.getParameter("createTime");
DeptDao dao=new DeptDaoImpl();
dao.addDept(new Dept(user, Date.valueOf(date)));
request.getRequestDispatcher("/showDept").forward(request,response);
}
}
查询:
public class ShowServlet extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
String user= (String) request.getAttribute("user");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
DeptDao dao=new DeptDaoImpl();
List<Dept> list=dao.findAllDept();
out.println("<table >");
for(Dept d:list){
out.println("<tr>");
out.println("<td>"+d.getDeptno()+"</td>");
out.println("<td>"+d.getDname()+"</td>");
out.println("<td>"+d.getCreateTime()+"</td>");
out.println("</tr>");
}
out.println("</table >");
out.close();
}
3.转发和重定向
服务器转发:
地址栏不会发生改变,在服务器端完成,效率高。
携带数据可以在Servlet之间进行传递
Plain Text
//请求转发,服务器转发
request.setAttribute("msg","欢迎你登陆成功");//request存储数据
request.getRequestDispatcher("/welcomeServlet").forward(request,response);
客户端重定向:
客户端重新发送新的请求,无法携带数据。地址栏会发生改变。
Plain Text
//客户端重定向
//服务器会向客户端浏览器发送一个响应:url, 客户端会重新请求该URL
request.setAttribute("msg","欢迎你登陆成功");//request存储数据
response.sendRedirect("/login.html");
转发:
发出了一次请求,地址栏不发生变化。可以携带数据
重定向:
发出了两次请求,地址栏发生变化,不可以携带数据
context
一个Servlet的get方法中获得ServletContext初始化参数,并将它存放到ServletContext;另一个Servlet的get方法中获取ServletContext中存储的属性
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<context-param>
<param-name>功夫</param-name>
<param-value>华山剑法</param-value>
</context-param>
</web-app>