Javaweb
2023-03-14 09:44:59 时间
发送错误页面
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; public class AServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.sendError(404, "不给你!!"); } }
显示图片
1 import java.io.FileInputStream; 2 import java.io.IOException; 3 4 import javax.servlet.ServletException; 5 import javax.servlet.http.HttpServlet; 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8 9 import org.apache.commons.io.IOUtils; 10 11 12 public class BServlet extends HttpServlet { 13 14 15 public void doGet(HttpServletRequest request, HttpServletResponse response) 16 throws ServletException, IOException { 17 /*String s = "Hellow outputStream"; 18 byte[] bytes = s.getBytes(); 19 response.getOutputStream().write(bytes);*/ 20 String path = "F:/521宿舍线上点单.png"; 21 /* 22 * 响应字节数据 23 * 把一张图片读到数组中 24 */ 25 FileInputStream in = new FileInputStream(path); 26 byte[] bytes = IOUtils.toByteArray(in);//读取输入流的字节到字节数组中 27 response.getOutputStream().write(bytes); 28 29 30 } 31 32 }
重定向
1 import java.io.IOException; 2 import java.io.PrintWriter; 3 4 import javax.servlet.ServletException; 5 import javax.servlet.http.HttpServlet; 6 import javax.servlet.http.HttpServletRequest; 7 import javax.servlet.http.HttpServletResponse; 8 9 10 public class CServlet extends HttpServlet { 11 12 /** 13 * The doGet method of the servlet. <br> 14 * 15 * This method is called when a form has its tag value method equals to get. 16 * 17 * @param request the request send by the client to the server 18 * @param response the response send by the server to the client 19 * @throws ServletException if an error occurred 20 * @throws IOException if an error occurred 21 */ 22 public void doGet(HttpServletRequest request, HttpServletResponse response) 23 throws ServletException, IOException { 24 System.out.println("CServlet"); 25 /* 26 * 重定向 1、设置Localhost 2、发送302状态码 27 */ 28 /*response.setHeader("Localhost", "DServlet"); 29 response.setStatus(302);*/ 30 31 /* 32 * 快捷重定向 33 */ 34 response.sendRedirect("www.baidu.com"); 35 } 36 37 }
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; public class DServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("DServlet"); } }
/** * 路径 * web.xml中<url-pattern>路径(它是Servlet路径) * 要么以"*"开头 要么以"/"开头 * 不以"/"开头:相对当前Servlet路径 * * 转发和包含路径 * ***以"/"开头 相对于当前路径 例如httpL//localhost:8080/项目名 request.getRequestdispacher("/Bservlet").for..(); * 不以"/"开头相当于Servlet路径 * * 重定向路径(客户端路径) * 以 "/" 开头相对当前主机 例如"http://localhost:8080" 所以需要自己手动添加项目名 reponse.sendRedirect("/day10_1/Servlet") * * 页面中超链接和表单路径 * 与重定向相同都是客户端路径,需要添加项目名 * * 建议使用"/" 开头的路径 即绝对路径 * * ServletContext获取资源路径() * 相当于项目目录 即当然index.jsp所在目录 * * ClassLoader获取资源路径 * 相对classes路径 * * class获取资源路径 * 以"/"开头相对classes目录 * 不以"/"开头相对于当前.class文件所在目录 */
相关文章
- 在 Go 里用 CGO?这 7 个问题你要关注!
- 9款优秀的去中心化通讯软件 Matrix 的客户端
- 求职数据分析,项目经验该怎么写
- 在OKR中,我看到了数据驱动业务的未来
- 火山引擎云原生大数据在金融行业的实践
- OpenHarmony富设备移植指南(二)—从postmarketOS获取移植资源
- 《数据成熟度指数》报告:64%的企业领袖认为大多数员工“不懂数据”
- OpenHarmony 小型系统兼容性测试指南
- 肯睿中国(Cloudera):2023年企业数字战略三大趋势预测
- 适用于 Linux 的十大命令行游戏
- GNOME 截图工具的新旧截图方式
- System76 即将推出的 COSMIC 桌面正在酝酿大变化
- 2GB 内存 8GB 存储即可流畅运行,Windows 11 极致精简版系统 Tiny11 发布
- 迎接 ecode:一个即将推出的具有全新图形用户界面框架的现代、轻量级代码编辑器
- loongarch架构介绍(三)—地址翻译
- Go 语言怎么解决编译器错误“err is shadowed during return”?
- 敏捷:可能被开发人员遗忘的部分
- Denodo预测2023年数据管理和分析的未来
- 利用数据推动可持续发展
- 在 Vue3 中实现 React 原生 Hooks(useState、useEffect),深入理解 React Hooks 的