import java.io.IOException;
import javax.servlet.RequestDispatcher;
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 com.sun.net.httpserver.Authenticator.Success;
//请求转发
/**
* Servlet implementation class LoginServlet
*/
@WebServlet("/LoginServlet.do")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public LoginServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("UTF-8");
// 获取请求过来的数据
String username=request.getParameter("userName");
String password=request.getParameter("passWord");
// 对应第二种 通过request 对象 存储获取的数据在转发 设置name=值
request.setAttribute("name", username);
request.setAttribute("paw", password);
if (username!=null&&username.equals("lovo")&&password!=null&&password.equals("123456")) {
// 获取请求转发器
RequestDispatcher requestDispatcher =request.getRequestDispatcher("/DongtaiLogin.do");
requestDispatcher.forward(request, response);
}else{
request.getRequestDispatcher("/shibai.html").forward(request, response);;
}
}
}
//接收在显示出来 这个连着上面的那个使用
import java.io.IOException;
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;
import org.apache.jasper.tagplugins.jstl.core.Out;
//动态的转发得到用户名响应
/**
* Servlet implementation class DongtaiLogin
*/
@WebServlet("/DongtaiLogin.do")
public class DongtaiLogin extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public DongtaiLogin() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
// 获取请求数据 request.getParameter("userName")括号里写的是表单定义的name名
// String username=request.getParameter("userName");
// String password=request.getParameter("passWord");
// 第二种
String username =(String)request.getAttribute("name");
String password =(String)request.getAttribute("paw");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>返回内容</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>"+username+"</h1>");
out.println("<h1>"+password+"</h1>");
out.println("/<body>");
out.println("/<html>");
out.close();
}
}