1.建一个类,获取用
package com.yin; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class RequestTest extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //System.out.println("进入这个请求"); //处理请求 String username=req.getParameter("username"); String password=req.getParameter("password"); System.out.println(username+":"+password); resp.sendRedirect("/response1_war/success.jsp"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { super.doGet(req, resp); } }
2.修改INDEX.JSP用它来录入用户名密码。
<html> <body> <h2>Servlet Demo!</h2> <%--这里提交的路径,需要寻找到项目路径--%> <%--${pageContext.request.contextPath}代表当前项目--%> <form action="${pageContext.request.contextPath}/login" method="get"> 用户名:<input type="text" name="username"><br> 密码:<input type="password" name="password"><br> <input type="submit"> </form> </body> </html>
3. .web.xml里面加入加入
<servlet> <servlet-name>request</servlet-name> <servlet-class>com.yin.RequestTest</servlet-class> </servlet> <servlet-mapping> <servlet-name>request</servlet-name> <url-pattern>/login</url-pattern> </servlet-mapping>
4.sucess网页用来验证
<html> <body> <h2>Success!</h2> </body> </html>
标签:用户名,resp,req,javax,密码,import,servlet,Response From: https://www.cnblogs.com/insoon/p/17052930.html