时间:2023-10-06 17:30:02 | 来源:网站运营
时间:2023-10-06 17:30:02 来源:网站运营
Java Web实验报告:Servlet容器模型:一、实验目的package com.demo;import java.io.*;import java.sql.*;import javax.servlet.*;import javax.servlet.http.*;public class ConfigDemoServlet extends HttpServlet{String servletName=null;ServletConfig config=null;String email=null;String telephone=null;public void init() {config = getServletConfig();servletName = config.getServletName();email = config.getInitParameter("email");telephone = config.getInitParameter("telephone");}public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException{ response.setContentType("text/html;charset=gb2312"); PrintWriter out = response.getWriter(); out.println("<html><body>"); out.println("Servlet名称:"+servletName+"<br>"); out.println("Email地址:"+email+"<br>"); out.println("电话:"+telephone); out.println("</body></html>");}}
【步骤2】修改web.xml文件,在其中添加下面Servlet定义和初始化参数定义<servlet> <servlet-name>ConfigDemoServlet</servlet-name> <servlet-class>Com.demo.ConfigDemoServlet</servlet-class> <init-param> <param-name>email</param-name> <param-value>XXXXXXX@qq.com</param-value> </init-param> <init-param> <param-name>telephone</param-name> <param-value>123456</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>ConfigDemoServlet</servlet-name> <url-pattern>/configDemo.do</url-pattern> </servlet-mapping>
【步骤3】在浏览器的地址栏中输入下面URL访问该Servlet:<context-param> <param-name>email</param-name> <param-value>1623093551@qq.com</param-value> </context-param> <context-param> <param-name>telephone</param-name> <param-value>1008611</param-value> </context-param>
在servlet文件中修改:package com.demo;import java.io.*;import java.util.*;import javax.servlet.*;public class ServerInfo extends GenericServlet { public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=GB2312"); PrintWriter out = response.getWriter(); ServletContext sc = getServletContext(); out.println("<html>"); out.println("<body>"); out.println("<head>"); out.println("<title>获取服务器端信息</title>"); out.println("</head>"); out.println("<body>"); out.println("<h2>服务器端信息:</h2>"); out.println("<table width=/"500/" border=/"1/">"); out.println("<tr>"); out.println("<td width=/"175/">站点名</td>"); out.println("<td width=/"325/">" + request.getServerName() + "</td>"); out.println("</tr>"); out.println("<tr>"); out.println("<td>端口号</td>"); out.println("<td>" + request.getServerPort() + "</td>"); out.println("</tr>"); out.println("<tr>"); out.println("<td>服务器类型</td>"); out.println("<td>" + sc.getServerInfo() + "</td>"); out.println("</tr>"); out.println("<tr>"); out.println("<td>支持Servlet版本</td>"); out.println("<td>"+sc.getMajorVersion()+"."+sc.getMinorVersion()+"</td>"); out.println("</tr>"); out.println("<tr>"); out.println("<td>服务器属性</td>"); out.println("<td>"); // 获得服务器属性集合 Enumeration attributes = sc.getAttributeNames(); while (attributes.hasMoreElements()) { String name = (String)attributes.nextElement(); out.println(name); } out.println("</td>"); out.println("</tr>"); out.println("</table>"); out.println("</body>"); out.println("</html>"); }}
运行结果:<html> <head> <title>User Information</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <h1>用户信息</h1> <form action="FirstServlet" method="post"> <p>学号:<input type="text" name="stu_id" /></p> <p>姓名:<input type="text" name="stu_name" /></p> <input type="submit" name="sub" value="提交"/> <input type="reset" name="button" value="重置"/> </form> </body></html>
运行结果:package com.demo.Student;public class Student { private String sno; //学号 private String name; //姓名 //访问学号 public String get_sno() { return sno; } //访问姓名 public String get_name() { return name; } //修改学号 public void set_sno(String v_sno) { this.sno=v_sno; }//修改姓名 public void set_name(String v_name) { this.name=v_name; }}
步骤三:package com.demo.Student;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletContext;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.annotation.WebServlet;import javax.servlet.RequestDispatcher;@WebServlet(name="FirstServlet",urlPatterns={"/FirstServlet"})public class FirstServlet extends HttpServlet { protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); try (PrintWriter out = response.getWriter()) { /* TODO output your page here. You may use following sample code. */ out.println("<!DOCTYPE html>"); out.println("<html>"); out.println("<head>"); out.println("<title>Servlet FirstServlet</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>Servlet FirstServlet at " + request.getContextPath() + "</h1>"); out.println("</body>"); out.println("</html>"); } } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("GB2312"); PrintWriter out = response.getWriter(); String stu_id=request.getParameter("stu_id"); String stu_name=request.getParameter("stu_name"); out.println("hello!"); Student stu=new Student(); stu.set_sno(stu_id); stu.set_name(stu_name); this.getServletContext().setAttribute("stu_id", stu.get_sno()); this.getServletContext().setAttribute("stu_name",stu.get_name()); RequestDispatcher report=request.getRequestDispatcher("/SecondServlet"); report.forward(request, response); } @Override public String getServletInfo() { return "Short description"; }// </editor-fold>}
SecondServlet中的doGet方法package com.demo.Student;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 = "SecondServlet", urlPatterns = {"/SecondServlet"})public class SecondServlet extends HttpServlet { protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); try (PrintWriter out = response.getWriter()) { /* TODO output your page here. You may use following sample code. */ out.println("<!DOCTYPE html>"); out.println("<html>"); out.println("<head>"); out.println("<title>Servlet SecondServlet</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>Servlet SecondServlet at " + request.getContextPath() + "</h1>"); out.println("</body>"); out.println("</html>"); } } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset = UTF-8"); PrintWriter out = response.getWriter(); Object stu_id=getServletContext().getAttribute("stu_id"); Object stu_name=getServletContext().getAttribute("stu_name"); out.println("学号:"+stu_id+"/n"); out.println("姓名:"+stu_name);out.println("<a href='E:/Javaweb/Application/build/web/input.html'>返回</a>"); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } @Override public String getServletInfo() { return "Short description"; }// </editor-fold>}
关键词:容器,模型,报告,实验