/javaee02/src/servlet/LoginServlet1.java
package servlet;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class LoginServlet1
*/
@WebServlet("/LoginServlet1") //servlet创建方式下 自带的路径
public class LoginServlet1 extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("utf-8"); //请求端对post请求产生的乱码处理
response.setContentType("text/html;charset=utf-8"); //响应端对乱码显示的处理
String name=request.getParameter("uname");
String psw=request.getParameter("psw");
if("admin".equals(name)&&"123".equals(psw)) {
request.getRequestDispatcher("success.jsp").forward(request, response);
// response.sendRedirect("success.jsp");
}
else {
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="LoginServlet1" method="get">
用户名:<input type="text" name="uname"/><br/>
密码:<input type="password" name="psw"/>
<input type="submit" />
</form>
</body>
</html>
success.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>测试转发与重定向</title>
</head>
<body>
<h2>登陆成功xxx</h2>
</body>
</html>