文章目录
【JavaWeb】servlet(上)
【JavaWeb】servlet(中)
【JavaWeb】servlet(下)
1.HttpServletRequest
1.1HttpServletRequest的作用
每次有请求进入Tomcat服务器的时候,Tomcat服务器就会把请求过来的HTTP协议信息解析封装好到Request对象中。然后传递到service方法(doGet和doPost)中。我们可以通过HttpServletRequest对象,获取所有请求信息。
1.2HttpServletRequest类的常用方法
方法 | 作用 |
---|---|
getRequestURI | 获取请求的资源路径 |
getRequestURL | 获取请求的统一资源定位符(绝对路径) |
getRemoteHost | 获取客户端的ip地址 |
getHeader | 获取请求头 |
getParameter | 获取请求的参数 |
getParameterValues | 获取请求的参数(多个值的时候使用) |
getMethod | 获取请求的方法GET或POST |
setAttribute(key, value) | 设置数据域 |
getAttribute(key) | 获取数据域 |
getRequestDispatcher | 获取请求转发对象 |
1.3HttpServletRequest实例
RequestAPIServlet.java
package com.sdnu.servlet;
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 RequestAPIServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("获取请求的资源路径:--->" + req.getRequestURI());
System.out.println("获取请求的统一资源定位符(绝对路径):--->" + req.getRequestURL());
System.out.println("获取客户端的ip地址:--->" + req.getRemoteHost());
System.out.println("获取请求头User-Agent:--->" + req.getHeader("User-Agent"));
System.out.println("获取请求的方法GET或POST:--->" + req.getMethod());
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>servlet3</servlet-name>
<servlet-class>com.sdnu.servlet.RequestAPIServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servlet3</servlet-name>
<url-pattern>/servlet3</url-pattern>
</servlet-mapping>
</web-app>
1.4获取请求的参数
ParameterServlet.java
package com.sdnu.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
public class ParameterServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String userName = req.getParameter("userName");
String passWord = req.getParameter("passWord");
String[] hobby = req.getParameterValues("hobby");
System.out.println("用户名:" + userName);
System.out.println("密码:" + passWord);
System.out.println("爱好:" + Arrays.asList(hobby));
}
}
form.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>form</title>
</head>
<body>
<form action="http://localhost:8080/servlet3_war_exploded/ParameterServlet" method="get">
用户名:<input type="text" name="userName"/><br/>
密码:<input type="password" name="passWord"/><br/>
兴趣爱好:<input type="checkbox" name="hobby" value="篮球"/>
篮球<input type="checkbox" name="hobby" value="足球"/>
羽毛球<input type="checkbox" name="hobby" value="羽毛球"/>
兵乓球<input type="checkbox" name="hobby" value="兵乓球"/>
<br/>
<input type="submit"/>
</form>
</body>
</html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>servlet3</servlet-name>
<servlet-class>com.sdnu.servlet.ParameterServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servlet3</servlet-name>
<url-pattern>/ParameterServlet</url-pattern>
</servlet-mapping>
</web-app>
注意:
解决post请求时出现乱码
req.setCharacterEncoding("UTF-8");
2.请求转发
Servlet1.java
package com.sdnu.servlet;
import javax.servlet.RequestDispatcher;
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 Servlet1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取请求的参数(办事的材料),并且查看
String userName = req.getParameter("userName");
System.out.println("在柜台一中查看参数:" + userName);
//给材料1改一个章,并且传递到Servlet2中,查看
req.setAttribute("key","柜台1的章");
//问路,Servlet2怎么走?
//请求转发必须以斜杠开头,/表示地址为 http://ip:port/工程名/ ,在idea中映射为web目录。
RequestDispatcher requestDispatcher = req.getRequestDispatcher("/servlet2");
//走向柜台2
requestDispatcher.forward(req,resp);
}
}
servlert2.java
package com.sdnu.servlet;
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 Servlet2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取请求的参数(办事的材料),并且查看
String userName = req.getParameter("userName");
System.out.println("在柜台二中查看参数:" + userName);
//产看柜台1是否有章
Object key1 = req.getAttribute("key");
System.out.println(key1);
//柜台2办理业务
System.out.println("柜台2办理自己的业务");
}
}
3.路径
3.1base标签
3.2.JavaWeb中的相对路径与绝对路径
3.3web中/斜杠的不同意义
- 在web中 / 斜杠是一种绝对路径。
- / 斜杠如果被浏览器解析,得到的地址是: http://ip:port/
<a href="/" />斜杠</a>
- / 斜杠如果被服务器解析,得到的地址是:http://ip:port/工程路径
1.<url-pattern>/servlet1</url-pattern>
2.servletContent.getRealPath("/");
3.request.getRequestDispatcher("/");
- 特殊情况
response.sendRediect("/"); 把斜杠发给浏览器解析,得到
http://ip:port/
4.HttpServletResponse
4.1HttpServletResponse类的作用
4.2两个输出流
示例
ResponseIOServlet.java
package com.sdnu.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class ResponseIOServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
PrintWriter write = resp.getWriter();
write.println("hello servlet!!!!");
}
}
4.3解决乱码问题
设置服务器的字符。
resp.setCharacterEncoding("UTF-8");
通过响应头,可以设置浏览器的字符。
resp.setHeader("Content-Type", "text/html; charset-UTF-8");
同时设置客户端和服务器的字符
resp.setContentType("text/html; charset-UTF-8");
4.4请求重定向
特点
示例
Response.java
package com.sdnu.servlet;
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 Response extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("重定向之前的第一次请求 Response");
resp.setStatus(302);
resp.setHeader("Location","http://localhost:8080/servlet3_war_exploded/response2");
}
}
Response2.java
package com.sdnu.servlet;
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 Response2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().println("Response2!!!!!");
}
}
地址栏变化如下
变化前:
http://localhost:8080/servlet3_war_exploded/response
变化后:
http://localhost:8080/servlet3_war_exploded/response2
请求重定向的第二种方式:
Response.java
package com.sdnu.servlet;
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 Response extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("重定向之前的第一次请求 Response");
//resp.setStatus(302);
//resp.setHeader("Location","http://localhost:8080/servlet3_war_exploded/response2");
resp.sendRedirect("http://localhost:8080/servlet3_war_exploded/response2");
}
}
作者:Beyong
出处:Beyong博客
github地址:https://github.com/beyong2019
本博客中未标明转载的文章归作者Beyong有,欢迎转载,但未经作者同意必须保留此段声明,且在文章明显位置给出原文连接,否则保留追究法律责任的权利。