<form action="tiao" method="get">html
username:<input type="text" name="username" value=""/> <br/>java
password:<input type="password" name="password" value=""/> <br/>web
<input type="submit" value="登陸">apache
</form>瀏覽器
Public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {tomcat
//跳轉到login安全
RequestDispatcher dis=req.getRequestDispatcher("login.html");服務器
dis.forward(req, resp);網絡
}app
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//getParameter根據html標籤中表單的name屬性獲取的
String username=req.getParameter("username");
String password=req.getParameter("password");
System.out.println(username+"+++"+password);
//判斷
if(("admin").equals(username)&&("123123").equals(password)) {
System.out.println("登陸成功!");
//登陸成功跳轉到index(首頁)
req.getRequestDispatcher("index.html").forward(req, resp);
}else {
System.out.println("登陸失敗!");
}
}
<servlet>
<servlet-name>TiaoServlet</servlet-name>
<servlet-class>com.jh.web.servlet.TiaoServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TiaoServlet</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<!-- ************************************* -->
<servlet>
<servlet-name>LoginServlst</servlet-name>
<servlet-class>com.jh.web.servlet.LoginServlst</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlst</servlet-name>
<!--訪問路徑要與表單中的action一致 -->
<url-pattern>/tiao</url-pattern>
</servlet-mapping>
<span> 登錄成功</span>