中建南方建设集团网站,贵州省建设厅建筑质监站网站,广州建设品牌网站,做网站一般多少【步骤 1】创建一个名为 input.html 的 HTML 页面#xff0c;其中包括一个表单#xff0c;表单中包含两 个文本域#xff0c;分别供用户输入学号和姓名#xff0c;该页面也包含提交和重置按钮。 【步骤 2】定义一个名为 com.demo.Student 类#xff0c;其中包括学号 sno 和…【步骤 1】创建一个名为 input.html 的 HTML 页面其中包括一个表单表单中包含两 个文本域分别供用户输入学号和姓名该页面也包含提交和重置按钮。 【步骤 2】定义一个名为 com.demo.Student 类其中包括学号 sno 和姓名 name 两个 private 的成员变量定义访问和修改 sno 和 name 的方法。 【步骤 3】编写名为 FirstServlet 的 Servlet要求当用户在 input.html 中输入信息后点击 “提交”按钮请求 FirstServlet 对其处理。在 FirstServlet 中使用表单传递的参数学号和 姓名创建一个 Student 对象并将其作为属性存储在 ServletContext 对象中然后获得通过 ServletContext 的 getRequestDispatcher()方法获得 RequestDispatcher()对象将请求转发到 SecondServlet。 【步骤 4】在 SecondServlet 中取出 ServletContext 上存储的 Student 对象并显示输出 该学生的学号和姓名。在 SecondServlet 的输出中应该包含一个超链接点击该连接可以返 回 input.html 页面。
源代码
Student类
public class Student {private String sno;private String name;public Student(String sno, String name) {this.sno sno;this.name name;}public String getSno() {return this.sno;}public void setSno(String sno) {this.sno sno;}public String getName() {return this.name;}public void setName(String name) {this.name name;}
}
FirstServlet类
public class FirstServlet extends HttpServlet {private static final long serialVersionUID 1L;public FirstServlet() {}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {ServletContext servletContext this.getServletContext();servletContext.setAttribute(stu, new Student(request.getParameter(sno), request.getParameter(name)));servletContext.getRequestDispatcher(/SecondServlet).forward(request, response);}public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this.doGet(request, response);}
}
SecondServlet类
public class SecondServlet extends HttpServlet {private static final long serialVersionUID 1L;public SecondServlet() {}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {response.setContentType(text/html;charsetUTF-8);ServletContext context this.getServletContext();Student student (Student)context.getAttribute(stu);PrintWriter out response.getWriter();String title 读取数据;String docType !DOCTYPE html \n;out.println(docType html\n headtitle title /title/head\n body bgcolor\#f0f0f0\\n h1 align\center\ title /h1\n ul\n lib学号/b student.getSno() \n lib姓名/b student.getName() \n /ul\n a href\http://localhost:8080/webDemo2_war_exploded/input.html\访问输入页/a /body/html);}public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {this.doGet(request, response);}
}