1.获取参数context-param中的参数的值
①配置web.xml
<!--context-param是上下文参数(它属于整个web工程)-->
<context-param>
<param-name>username</param-name>
<param-value>context</param-value>
</context-param>
<!--context-param是上下文参数(它属于整个web工程)-->
<context-param>
<param-name>password</param-name>
<param-value>root</param-value>
</context-param>
②创建contestServlet类继承HttpServlet类获取值
import javax.servlet.ServletContext;
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 contextServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1.获取web.xml中配置的上下文 参数context-param
ServletContext context = getServletConfig().getServletContext();
String username = context.getInitParameter("username");
System.out.println("context-pram参数的username值是:"+username);
System.out.println("context-pram参数的password值是:"+context.getInitParameter("password"));
//2.获取当前的工程路径,格式:/工程路径
System.out.println("当前的工程路径:"+context.getContextPath());
//3.获取工程部署后在服务器硬盘上的绝对路径
System.out.println("工程部署的路径是:"+context.getRealPath("/"));
}
}
③结果
2.什么是servletcontext?
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/*
什么是servletcontext?
1.servletcontext是一个接口,他表示servlet上下文对象
2.一个web工程,只有一个servletcontext对象的实例
3.servletcontext对象是一个域对象
4.servletcontext是在web工程部署启动的时候创建,在web工程停止的时候销毁
*/
public class contextServlet1 extends HttpServlet {
//ServletContext像map一样存取数据
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取servletcontext的对象
ServletContext servletContext = getServletContext();
System.out.println("保存之前:context1获取的key1值是:"+servletContext.getAttribute("key1"));
servletContext.setAttribute("key1","value1");
System.out.println("context中获取域数据key1的值是:"+servletContext.getAttribute("key1"));
}
}
3.Request相关API
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 {
//1.getRequestURI() 获取请求的资源路径
System.out.println("URL-->"+req.getRequestURI());
//2.getRequestURL() 获取请求的统一资源定位符
System.out.println("URL-->"+req.getRequestURL());
//3.getRemoteHost() 获取客户端的ip地址
System.out.println("ip地址"+req.getRemoteHost());
//4.getHeader() 获取请求头
System.out.println("请求头:"+req.getHeader("user-agent"));
//5.getMethod() 获取请求的方式GET或Post
System.out.println(req.getMethod());
结果
4.从form表单中获取相关参数
①form.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="http://localhost:8080/07servlet_war_exploded/parameter" method="get">
用户名:<input type="text" name="username"><br/>
密码:<input type="password" name="password"><br/>
兴趣爱好:<input type="checkbox" name="hobby" value="cpp">c++
<input type="checkbox" name="hobby" value="java">java
<input type="checkbox" name="hobby" value="js">javascript<br/>
<input type="submit">
</form>
</body>
</html>
②parameterServlet类
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;
import java.util.Arrays;
public class parameterServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("--------doget---------");
//获取请求参数
String username = req.getParameter("username");
String password = req.getParameter("password");
String[] hobbies = req.getParameterValues("hobby");
System.out.println("用户名"+username);
System.out.println("密码"+password);
System.out.println("兴趣爱好"+ Arrays.asList(hobbies));
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置请求体的字符集为UTF-8,从而解决post请求的中文乱码问题
//也要在获取请求参数之前调用才有效
req.setCharacterEncoding("UTF-8");
System.out.println("--------dopost---------");
//获取请求参数
String username = req.getParameter("username");
String password = req.getParameter("password");
String[] hobbies = req.getParameterValues("hobby");
System.out.println("用户名"+username);
System.out.println("密码"+password);
System.out.println("兴趣爱好"+ Arrays.asList(hobbies));
}
}
③配置web.xml文件
<servlet>
<servlet-name>parameterServlet</servlet-name>
<servlet-class>com.atshangqiu.com.parameterServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>parameterServlet</servlet-name>
<url-pattern>/parameter</url-pattern>
</servlet-mapping>
④结果
5.请求转发
①创建servlet1和servlet2,并配置web.xml
<servlet>
<servlet-name>servlet1</servlet-name>
<servlet-class>com.atshangqiu.com.servlet1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servlet1</servlet-name>
<url-pattern>/servlet1</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>servlet2</servlet-name>
<servlet-class>com.atshangqiu.com.servlet2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servlet2</servlet-name>
<url-pattern>/servlet2</url-pattern>
</servlet-mapping>
②servlet1
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("在Servlet1(柜台1)中查看参数(材料)"+username);
//给材料盖一个章,并传递到servlet2
req.setAttribute("key1","柜台1的章");
//问路:Servlet2(柜台2)怎么走
/*
请求转发:必须要以斜杠打头,/表示地址为:http://ip:port/工程名/,映射到IDE代码的web目录
*/
RequestDispatcher requestDispatcher = req.getRequestDispatcher("/servlet2");
//走向servlet2
requestDispatcher.forward(req,resp);
}
}
③servlet2
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/*
请求转发的特点:
1.浏览器地址栏没有变化
2.他们是一次请求
3.他们共享Request域中的数据
4.可以转发到WEB-INF目录下
*/
public class servlet2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获取请求的参数(办事的材料)查看
String username = req.getParameter("username");
System.out.println("在Servlet2(柜台2)中查看参数(材料):"+username);
//查看柜台1是否盖章
Object key1 = req.getAttribute("key1");
System.out.println("柜台1是否有盖章:"+key1);
//处理自己的业务
System.out.println("Servlet2处理自己的业务");
}
}
④结果
在浏览器中输入http://localhost:8080/07servlet_war_exploded/servlet1?username=chenbin
结果如下:
6.response乱码问题
默认doget方法
responseIOservlet类
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 {
/* 方式一:
//设置服务器字符集为UTF-8
resp.setCharacterEncoding("UTF-8");
//通过响应头,设置浏览器也使用UTF-8字符集
resp.setHeader("Content-type","text/html;charset=UTF-8");
*/
//方式二:他会同时设置服务器和客户端都使用UTF-8字符集,还设置了响应头
//此方法一定要在获取流对象之前调用才有效
resp.setContentType("text/html;charset=UTF-8");
//要求往客户端回传字符串 数据
PrintWriter writer = resp.getWriter();
writer.write("陈彬很帅");
}
}
web.xml配置省略
7.请求重定向
response1
/*
请求重定向的特点
1.浏览器地址栏会发生变化
2.两次请求
3。不共享request域中的数据
4.不能访问WEB-INF喜爱的资源
5.可以访问工程 外的资源
*/
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 response1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("该域名已消失");
/*
方式一:
//设置响应状态码302,表示重定向,(已搬迁)
resp.setStatus(302);
//设置响应头,说明新的地址在哪里
resp.setHeader("Location","http://localhost:8080/07servlet_war_exploded/reponse2");
*/
//方式二:
resp.sendRedirect("http://localhost:8080/07servlet_war_exploded/reponse2");
}
}
response2
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 reponse2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().write("reponse2.result");
}
}
web.xml相关配置
<servlet>
<servlet-name>response1</servlet-name>
<servlet-class>com.atshangqiu.com.response1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>response1</servlet-name>
<url-pattern>/response1</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>response2</servlet-name>
<servlet-class>com.atshangqiu.com.reponse2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>response2</servlet-name>
<url-pattern>/reponse2</url-pattern>
</servlet-mapping>