先新建一个动态WEB项目。
展开WebRoot/index.jsp
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
2 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
3 <html>
4 <body>
5 <form action="${pageContext.request.contextPath}/Controller/servlet_Con" method="post">
6 用户名:<input type="text" name="username">
7 密 码:<input type="password" name="password">
8 <input type="Submit" value="提交" name="Submit" >
9 </form>
10 </body>
11 </html>
新建一个servlet.
1 package Controller;
2
3 import java.io.IOException;
4
5 import javax.servlet.ServletException;
6 import javax.servlet.http.HttpServlet;
7 import javax.servlet.http.HttpServletRequest;
8 import javax.servlet.http.HttpServletResponse;
9 //火影
10 public class servlet_Con extends HttpServlet {
11 private static final long serialVersionUID = 1L;
12 private String message;
13 public void doGet(HttpServletRequest request, HttpServletResponse response)
14 throws ServletException, IOException {
15
16 String name= request.getParameter("username");
17 String password=request.getParameter("password");
18 if("admin".equals(name)&&"123".equals(password))
19 {
20 message="密码正确,跳转成功";
21 System.out.println(message);
22 request.setAttribute( "message",message);
23 request.getRequestDispatcher("/success/success.jsp").forward(request, response);
24 }
25 else
26 {
27 message="用户名或密码错误,跳转错误";
28 System.out.println(message);
29 request.setAttribute( "message",message);
30 request.getRequestDispatcher("/error/error.jsp").forward(request, response);
31 }
32 }
33 public void doPost(HttpServletRequest request, HttpServletResponse response)
34 throws ServletException, IOException {
35 this.doGet(request, response);
36
37 }
38
39 }
分别建立两个。success.jsp error.jsp文件。用于,跳转页面。
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
2 <%
3 String path = request.getContextPath();
4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
5 %>
6
7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
8 <html>
9 <head>
10 <base href="<%=basePath%>">
11 <title>My JSP 'error.jsp' starting page</title>
12 </head>
13 <body>
14 ${message }//接收servlet过来的值。
15 <br>
16 </body>
17 </html>
测试:
1.先访问看看。
2.前台页面。自动跳转。
3.后台控制台。
----------------------------------------------------------------------------------------------------
The end...........Jsp 其实也要编译成servlet文件。