Response 写验证码:
package com.demo.response;import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.IOException;
import java.nio.IntBuffer;
import java.util.Random;public class Image extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//浏览器5s刷新resp.setHeader("refresh","5");//在内存中创建一个图片,设置宽高颜色BufferedImage bufferedImage = new BufferedImage(80,20,BufferedImage.TYPE_INT_RGB);//得到图片 Graphics 是一支笔Graphics2D graphics = (Graphics2D) bufferedImage.getGraphics();//设置图片的背景颜色graphics.setColor(Color.black);graphics.fillRect(0,0,80,20); //填充//给图片写数据graphics.setColor(Color.white);graphics.setFont(new Font(null,Font.ITALIC,20));graphics.drawString(num(),0,20);//告诉浏览器,这个请求用图片的方式打开resp.setContentType("image/jpg");//网站存在缓存,不让浏览器缓存resp.setDateHeader("expores",-1);resp.setHeader("Cache-Control","no-cache");resp.setHeader("Pragma","no-cache");//把图片写给浏览器ImageIO.write(bufferedImage,"jpg",resp.getOutputStream());}//生成随机数private String num(){Random random = new Random();String num = random.nextInt(9999)+"";StringBuffer stringBuffer = new StringBuffer();for (int i = 0; i < 4-num.length(); i++) {stringBuffer.append("0"); //不足用0填充数字}return stringBuffer.toString() + num;}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);}
}
记得先注册后运行
<servlet><servlet-name>img</servlet-name><servlet-class>com.demo.response.Image</servlet-class></servlet><servlet-mapping><servlet-name>img</servlet-name><url-pattern>/img</url-pattern></servlet-mapping>
Response 重定向:
一个 web 资源接收到客户端的请求后,它会通知客户端去访问另一个 web 资源,这个过程叫做重定向
sendRedirect 重定向
public class Redirect extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//resp.sendRedirect("/response/index.jsp");resp.sendRedirect("/response/img"); //重定向}
注意路径前要加自己命名的路径
web.xml 注册:
<servlet><servlet-name>re</servlet-name><servlet-class>com.demo.response.Redirect</servlet-class></servlet><servlet-mapping><servlet-name>re</servlet-name><url-pattern>/re</url-pattern></servlet-mapping>
浏览器输入后缀,将会跳转到另一个页面,地址栏改变,实现重定向
实现原理:
resp.setHeader("Location","/response/img");
resp.setStatus(302); //302重定向
一个响应头 Location、一个状态码 302
重定向和转发的区别:
相同点:页面都会实现跳转
不同点:
1.请求转发的时候,url 不会变化
2.重定向时,url 地址栏会发生变化
写个简单的提交重定向
修改 index.jsp
<%-- ${pageContext.request.contextPath}代表当前的项目 --%>
<form action = "${pageContext.request.contextPath}/login" method = "get">username: <input type = "text" name = "username"> <br>password: <input type = "password" name = "password"> <br><input type = "submit">
</form>
代码如下:
package com.demo.response;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 Request extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {//处理请求String username = req.getParameter("username");String password = req.getParameter("password");resp.sendRedirect("/response/success.jsp");}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);}
}
再写个跳转的 success.jsp
web.xml 注册:
<servlet><servlet-name>login</servlet-name><servlet-class>com.demo.response.Request</servlet-class></servlet><servlet-mapping><servlet-name>login</servlet-name><url-pattern>/login</url-pattern></servlet-mapping>
注意 <url-pattern> 的路径名与 index.jsp 里的 ${pageContext.request.contextPath} 保持一致
运行,提交后进入 success 页面