一、Servlet
package com.test;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(name = "Servlet_3",urlPatterns = "/login")
public class Servlet_3 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = "wuxiaowei";
String pawd ="123456";
String username = request.getParameter("name");
String userpwd = request.getParameter("pwd");
if(!username.equals(name)){
request.setAttribute("errormessage","账户错误");
request.getRequestDispatcher("/loginerror.jsp").forward(request,response);//转发
}
else if(!userpwd.equals(pawd)){
request.setAttribute("errormessage","密码错误");
request.getRequestDispatcher("/loginerror.jsp").forward(request,response);
}
else{
//重定向的两种方式
// response.setStatus(302);
// response.setHeader("location","http://www.baidu.com");
response.sendRedirect(".loginSucess.html");//等于上面两行的结合
}
}
}
二、html
login
<body>
<h2>用户登录</h2>
<form action="login" method="get">
<p>账号:<input type="text" name="name"/></p>
<p>密码:<input type="text" name="pwd"/></p>
<input type="submit" value="登录">
</form>
</body>
loginerror.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<%----%>
<meta http-equiv="refresh" content="3;url=/login.html" >
</head>
<body>
<h2>error</h2>
<div>
<%--<%=request.getAttribute("errormessage")%>--%>
<%=request.getAttribute("errormessage")%>
</div>
</body>
</html>
sucess
<body> <p>登录成功</p> </body>
1万+

被折叠的 条评论
为什么被折叠?



