一.学习内容
(1)解决响应乱码问题
提供解决乱码的方案:response.setContentType(“text/html;charset=utf-8”);
(2)实现网页定时刷新跳转
(3)HttpServletRedquest对象
获取请求行
获取请求列
获取请求消息头
(4)请求转发
(5)实现请求重定向
二.代码如下:
(1)解决响应乱码
package Demo;
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;
/**
* Servlet implementation class ChineseServlet
*/
@WebServlet({ "/ChineseServlet", "/chinese" })
public class ChineseServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
String data="中国-大数据产业";
System.out.println("chinses 收到前端请求,打印大数据");
PrintWriter pw=response.getWriter();
pw.println(data);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
(2)实现请求重定向
package Demo;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class LoginServlet
*/
@WebServlet({ "/LoginServlet", "/login" })
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("前端请求login,而不是logindashuju.html");
response.sendRedirect("/day_10.21/logindashuju.html");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
(3)消息头
package Demo;
import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class RedquestHeaderServlet
*/
@WebServlet( "/requesthead ")
public class RedquestHeaderServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
Enumeration<String> er=request.getHeaderNames();
while(er.hasMoreElements()) {
String name=er.nextElement();
String value =request.getHeader(name);
response.getWriter().print("消息头:"+name+""+value);//送回网页前端
System.out.println("消息头:"+name+""+value);//在服务器后端打印
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
(4)消息行
package Demo;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class RedquestLineServlet
*/
@WebServlet({ "/tequestline" })
public class RedquestLineServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String method =request.getMethod();
System.out.println("请求方式:"+method);
String proto=request.getProtocol();
System.out.println("请求协议:"+proto);
String contextPath =request.getContextPath();
System.out.println("项目路径:"+contextPath);
String servlePath=request.getServletPath();
System.out.println("servlet路径:"+servlePath);
String pathURI =request.getRequestURI();
StringBuffer pathURL=request.getRequestURL();
System.out.println("URI"+pathURI+"URL"+pathURL);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
(5)定时刷新跳转
package Demo;
import java.io.IOException;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class RefreshServlet
*/
@WebServlet({ "/RefreshServlet", "/refresh" })
public class RefreshServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
static int num =0;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("收到请求" +num);
response.setHeader("Refresh", "5");
response.getWriter().print(new Date());
num++;
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
(6)获取请求参数
package Demo;
import java.io.IOException;
import java.util.Arrays;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class RequestParamServlet
*/
@WebServlet({ "/RequestParamServlet", "/RequestParam" })
public class RequestParamServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("收到前端的请求");
String username=request.getParameter("username");
String ps=request.getParameter("password");
System.out.println("用户名:"+username);
System.out.println("密码:"+ps);
String [] hobbys=request.getParameterValues("hobby");
System.out.println("hobby:"+Arrays.toString(hobbys));
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
request.setCharacterEncoding("utf-8");
String username=request.getParameter("username");
System.out.println(username);
}
}