HttpServletRequest中文乱码处理、防盗链的使用、request的请求转发
中文乱码处理:
我们的代码主要是在form.html文件里面提供get和post进行数据发送,并在RequestDemo.java文件里面获取数据并输出至控制台
form.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <form action="/day02/RequestDemo" method="post"> 用户名1:<input name="username" type="text"><br/> <input type="submit" value="提交"><br/> </form> <form action="/day02/RequestDemo" method="get"> 用户名2:<input name="username" type="text"><br/> <input type="submit" value="提交"><br/> </form> <!--超链接提交的中文,服务器要想不乱吗,也只能手动处理 --> <a href="/day02/RequestDemo1?username=中国">点点</a> </body> </html>
RequestDemo.java
package test; import java.io.IOException; import java.io.UnsupportedEncodingException; 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 RequestDemo */ @WebServlet("/RequestDemo") public class RequestDemo 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 { // TODO Auto-generated method stub //test2(request); test3(request, response); } //对于post请求,下面这个函数依然可以正常显示中文 private void test3(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException, IOException { request.setCharacterEncoding("UTF-8"); String username = request.getParameter("username"); response.setCharacterEncoding("gb2312"); response.setContentType("text/html;charset=gb2312"); response.getWriter().write(username); } //解决post乱码问题 private void test2(HttpServletRequest req) throws UnsupportedEncodingException { req.setCharacterEncoding("UTF-8"); //只对post有效 String username = req.getParameter("username"); System.out.println(username); } //手动处理乱码 private void test1(HttpServletRequest req) throws UnsupportedEncodingException { String username = req.getParameter("username"); //getBytes("UTF-8"),因为我这个版本request内置编码就是UTF-8,所以getBytes里面就写UTF-8 username = new String(username.getBytes("UTF-8"),"UTF-8"); System.out.println(username); } /** * @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); } }
项目目录:
启动服务器后访问链接为:http://localhost:8080/day02/test.html
防盗链实现:
防盗链就是不允许其他用于直接访问此网站的某个页面等信息。你必须是上一个访问的页面是本网站才可以去访问这个页面信息
RequestDemo1.java实现防盗链逻辑控制
package test; 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 RequestDemo1 */ @WebServlet("/RequestDemo1") public class RequestDemo1 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 { // TODO Auto-generated method stub String referer = request.getHeader("referer"); if(referer==null || !referer.startsWith("http://localhost")) { //如果你上一个页面不是本网站就让你强制跳转到本网站其他网页 response.sendRedirect("/day02/index.jsp"); return; } String data = "防盗内容"; response.getWriter().write(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); } }
index.jsp文件
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="/day02/form.html">查看图书</a>
<br/>看广告<br/>
<a href="/day02/RequestDemo1">跳转到防盗内容所在页面</a>
</body>
</html>
如果你访问http://localhost:8080/day02/RequestDemo1,那么你会被强制跳转到http://localhost:8080/day02/index.jsp
request的请求与转发:
下面是一个请求与转发的示例:由于RequestDemo.java文件内不适合做数据输出(太麻烦),所以常采用jsp来输出数据,这个时候我们需要用到request域来传递数据用jsp来输出页面。
RequestDemo.java
package test; import java.io.IOException; import java.io.UnsupportedEncodingException; 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 RequestDemo */ @WebServlet("/RequestDemo") //MVC M(mode) javabean封装数据 V(view) 展示数据 C(control) servlet数据控制 //request请求转发数据: //由于servlet.java文件内不适合做数据输出(太麻烦),所以常采用jsp来输出数据,这个时候我们 //需要用到request域来传递数据 public class RequestDemo 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 { // TODO Auto-generated method stub String data="xxxxxx"; request.setAttribute("data", data); //第一个参数是jsp文件中取用的名字 //request也可以实现转发 request.getRequestDispatcher("/message.jsp").forward(request, response); } /** * @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); } }
message.jsp文件
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="java.util.*"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <% String data = (String)request.getAttribute("data"); out.write(data); %> </body> </html>
访问http://localhost:8080/day02/RequestDemo就可以了。下面说一下request请求转发的一些注意事项
这里我们说一下request的include方法,虽然Java文件内不适合输出html页面,但是之后某些地方会用到。
比如一个网站的大多页面都是用同样的页头和页尾,那么就可以把页头,页尾写成单独两个head.jsp、foot.jsp文件,之后在Java文件中引用它,这样我们只需要构造页面中间部分就可以。
但是要注意,如果你的每一个jsp页面都有html页面框架,你返回给浏览器的页面源码就会出现下面情况
所以我们建议jsp文件不要包含html框架,像下面一样
我们把框架单独写出来