时间:2023-10-06 17:18:02 | 来源:网站运营
时间:2023-10-06 17:18:02 来源:网站运营
Java Web实验报告:HTTP的响应:第二部分 HTTP的响应package com.demo;import java.io.IOException;import java.io.PrintWriter;import java.text.SimpleDateFormat;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;@WebServlet(name = "RefreshServlet", urlPatterns = {"/RefreshServlet"})public class RefreshServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=gb2312"); response.setHeader("refresh","3"); PrintWriter out = response.getWriter(); Date date = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd a HH:mm:ss"); String s = sdf.format(date); out.println("<html>"); out.println("<body>"); out.println("<h3 align=/"center/">每3秒钟刷新一次页面</h3><p>"); out.println("<h3 align=/"center/">现在的时间是:"+s+"</h3><p>"); out.println("</body>"); out.println("</html>"); } }
package com.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;@WebServlet(name = "WordServlet", urlPatterns = {"/WordServlet"})public class WordServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setHeader("Content-Encoding","gb2312"); //设置响应的内容类型 response.setContentType("application/msword;charset = UTF-8"); PrintWriter out = response.getWriter(); out.println("这是一个word文件,你可以在该文件上编辑实现word文档的功能"); }}
(2)运行WordServlet.java文件,下载“WordServlet”的文件,并用“Microsoft Office Word”打开该文件<html> <head> <title>兴趣爱好</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <form action="hobby.do" method="get"> 姓名:<input type="text" name="name"><br> 班别:<input type="text" name="class"><br> <p>兴趣: <input type="radio" name="hobby" value="1">编程 <input type="radio" name="hobby" value="2">新闻 <input type="radio" name="hobby" value="3">游戏 <br> <input type="submit" value="提交"> <input type="reset" value="重置"> </body></html>
package com.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;@WebServlet(name = "HobbyServlet", urlPatterns = {"/hobby.do"})public class HobbyServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String hobby = request.getParameter("hobby"); //根据兴趣决定响应重定向的网站 if(hobby!=null&&hobby.equals("1")){ response.sendRedirect("http://www.csdn.net/?ref=toolbar"); }else if(hobby!=null&&hobby.equals("2")){ response.sendRedirect("http://news.163.com/"); }else if(hobby!=null&&hobby.equals("3")){ response.sendRedirect("http://www.4399.com/"); }else {//如果没有填写兴趣选项,则向客户端发送状态码和错误信息 response.sendError(404,"没有填写兴趣选项"); } } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }}
关键词:响应,报告,实验