文章目录
Request
request对象的一些方法(不显示web.xml的配置)
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 RequestDemo01 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.获得web项目路径
System.out.println(request.getContextPath());
//2.请求的URL路径
System.out.println(request.getRequestURI());
//3.获得Remote 远程的用户名
System.out.println(request.getRemoteUser());
//4.获得SessionId;
System.out.println(request.getRequestedSessionId());
//5.请求servlet的url
System.out.println(request.getServletPath());
//6.获得本地地址 防火墙
System.out.println(request.getLocalAddr());
//7.获得本地名字
System.out.println(request.getLocalName());
//8.获得访问的端口号
System.out.println(request.getLocalPort());
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
}
运行结果(sout-输出到output):
/JSP
/JSP/r
null
1FDF0DEDAD84D55F0BA6D705E4CE0DCF
/r
0:0:0:0:0:0:0:1
0:0:0:0:0:0:0:1
8080
Request对象接收前段用户提交的参数
1.写前端页面
-
JSP取得项目的路径
${pageContext.request.contextPath}
-
Form表单
- action:表单要提交的位置-服务项目的路径/servlet对应的请求url
- method:提交表单的方式-post/get
-
/web 下new一个Register.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>注册</title> </head> <body> <h1>注册</h1> <form action="${pageContext.request.contextPath}/r1" method="get"> <p>用户名:<input type="text" name="username"></p> <p>密码:<input type="password" name="password"></p> <p> <input type="submit"> <input type="reset"> </p> </form> </body> </html>
2.写处理前端提交信息的Servlet
package priv_sehun_servlet3;
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 RequestDemo1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException