http头协议
凡是以超链接登录的都是doget,表单请求可以设置请求头协议:doget、dopost
doget请求,可以直接用url带参数访问页面文件,如:http://127.0.0.1:8080/helloWeb_jun/login.jsp?name=aaa&pwd=123123123
而如果在表单中设置了method="post"
的话则不能用上面的方式访问,必须是通过超链接点击访问。
浏览器的兼容: 我们要设置response.setContentType();用来设置头协议中的key,value。如:response.setContentType("text/html;charset=utf-8");
来指定编码、文件类型等属性。
要做只能从当前页面跳转 判断拿到的referer是否是null,是否是从当前站点跳转过去的。`String refer=request.getHeader(“Referer”);
String str = "";
if(refer==null){
str = "看点广告";
}else{
if(refer.contains("helloWeb")){
str = "<img src=‘./imgs/1.jpg‘>";
}else{
str = "请从主页点击查看";
}
}`
转发与重定向:
重定向:相当于做两次请求 第一次请求是为了拿到资源的位置,第二次才是直接访问资源 没有第一次请求的request和response了
重定向用到的方法是
String path = "/helloWeb_jun/jsps/result.jsp";
response.sendRedirect(path);
转发:做一次请求 但是服务器内部会有一次转发,将请求转到资源 这时,保留原来的request和response 。
转发用到的方法是:
String path ="/jsps/result.jsp";
request.getRequestDispatcher(path).forward(request, response);
gzip压缩
以压缩格式,要设
response.setContentType("text/html");
response.setHeader("Content-Encoding", "gzip");
response.setHeader("Content-length", ""+dest.length);
这里可以用setHeader的key,value来设置,也可以用response.setContentLength(dest.length);
来设置。
压缩
声明一点,如果是很小的文件,那么压缩之后反而会更大,因为压缩有开销。
用
ByteArrayOutputStream array = new ByteArrayOutputStream();
GZIPOutputStream gout= new GZIPOutputStream(array);
gout.write(str);
gout.close();
来压缩,最后从array返回出去的就是压缩好的字节数组。
创建Servlet的几种方法:
1、直接new Servlet。—这种不需要手动配置xml
2、用implements Servlet ,然后补全来写。–这个需要自己手动去xml文档中配置
3、用extends HttpServlet 然后覆盖所需要的函数来写—这个需要手动去xml文档中配置
配置类似如下所示:
<servlet>
<servlet-name>imgServlet</servlet-name>
<servlet-class>cn.hncu.imgServlet.imgServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>imgServlet</servlet-name>
<url-pattern>/servlet/imgServlet</url-pattern>
</servlet-mapping>
这个
<url-pattern>/servlet/imgServlet</url-pattern>
里面的/servlet/imgServlet
就是我们在表单中的action的值。
最后,本文代码如下:
GzipServlet
package cn.hncu.imgServlet;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.zip.GZIPOutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class GzipServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String msg = "dfjwoefjalsdjfowejflasjdfiowe放假偶尔发接哦我IQ耳机佛全文见佛千万IE附件分接欧文积分fjweofjwoefjwofj";
byte[] str = msg.getBytes();
//将字节数组压到array内存中去
ByteArrayOutputStream array = new ByteArrayOutputStream();
GZIPOutputStream gout= new GZIPOutputStream(array);
gout.write(str);
gout.close();
//将压缩后的数据从array中拿出
byte[] dest = array.toByteArray();
response.setContentType("text/html");
//response.setContentLength(dest.length);
response.setHeader("Content-Encoding", "gzip");
response.setHeader("Content-length", ""+dest.length);
OutputStream out = response.getOutputStream();
out.write(dest);
out.flush();
out.close();
}
}
DispatcherServlet
package cn.hncu.imgServlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 演示转发
* @author <a href="mailto:[email protected]">军街</a>
*
* @version 1.0 2016-7-17
*/
public class DispatcherServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String path ="/jsps/result.jsp";
request.getRequestDispatcher(path).forward(request, response);
}
}
RedirectServlet
package cn.hncu.imgServlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 演示重定向
* @author <a href="mailto:[email protected]">军街</a>
*
* @version 1.0 2016-7-17
*/
public class RedirectServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//法1:
String path = "/helloWeb_jun/jsps/result.jsp";
response.sendRedirect(path);
//法2:
//response.setStatus(302);
//response.setHeader("Location", path);
// response.setCharacterEncoding("utf-8");
// response.setCharacterEncoding("gbk");
//演示状态码
// response.setStatus(404);
// response.getWriter().write("not found找不到 ,good job");
}
}
ShowPicServlet
package cn.hncu.imgServlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ShowPicServlet implements Servlet{
@Override
public void destroy() {
}
@Override
public ServletConfig getServletConfig() {
return null;
}
@Override
public String getServletInfo() {
return null;
}
@Override
public void init(ServletConfig arg0) throws ServletException {
}
@Override
public void service(ServletRequest arg0, ServletResponse arg1)
throws ServletException, IOException {
HttpServletRequest request = (HttpServletRequest) arg0;
HttpServletResponse response = (HttpServletResponse) arg1;
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
//String str = "<img src=‘./imgs/1.jpg‘>";
//做只能从主页点击才能进入 靠单纯的url不能查看图片
//http://127.0.0.1:8080/helloWeb_jun/pic ----看点广告
String refer=request.getHeader("Referer");
String str = "";
if(refer==null){
str = "看点广告";
}else{
if(refer.contains("helloWeb")){
str = "<img src=‘./imgs/1.jpg‘>";
}else{
str = "请从主页点击查看";
}
}
out.write(str);
}
}
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<a href="jspDay1/demo1.jsp">导向我的jsp文件</a> <br>
<hr/>
<!--以form表单提交 这种是在url中显示输入的-->
<!-- 默认的method是get -->
<!-- http://127.0.0.1:8080/helloWeb_jun/login.jsp?name=aaa&pwd=123123123 -->
<form action="login.jsp">
姓名:<input type="text" name="name"/><br/>
密码:<input type="text" name="pwd"/><br/>
<input type="submit" value="登录"/>
</form>
<!--以form表单提交 这种是在url中不显示输入的-->
<!-- http://127.0.0.1:8080/helloWeb_jun/login.jsp -->
<form action="login.jsp" method="post">
姓名:<input type="text" name="name"/><br/>
密码:<input type="text" name="pwd"/><br/>
<input type="submit" value="登录"/>
</form>
<br/>
<!--以超链接方式登录-->
<a href="http://127.0.0.1:8080/helloWeb_jun/login.jsp?name=cv&pwd=6666">以超链接方式登录</a><br/>
<br/>
<br/>
<hr/>
<a href="pic">查看美女图</a>
<br/>
<br/>
<hr/>
<h2>下面演示重定向和转发</h2>
<!-- 重定向:相当于做两次请求 第一次请求是为了拿到资源的位置,第二次才是直接访问资源 没有第一次请求的request和response了-->
<!-- 转发:做一次请求 但是服务器内部会有一次转发,将请求转到资源 这时,保留原来的request和response -->
<a href="redirect">重定向</a><br/>
<a href="dispatcher">转发</a><br/>
<hr/>
<h2>演示压缩gzip</h2>
<!-- http://127.0.0.1:8080/helloWeb_jun/ -->
<a href="gzip">压缩gzip</a>
</body>
</html>
result.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1>这时重定向的最终页面</h1>
</body>
</html>
时间: 2024-10-07 12:09:13