佛山科学技术学院
实  验  报  告
课程名称                        Web编程与设计                 
实验项目      实验三  在线教学网站的开发——学生信息的录入       
专业班级    姓 名     学 号  
指导教师          成 绩           日 期                
                                                                           
一、实验目的
1、掌握JavaBean的使用;
2、 理解MVC开发模式。
二、实验内容
按照MVC模式开发的一般步骤,实现在线教学网站系统,完成学生信息录入和显示功能。
1)学生信息录入功能
在JSP页面中录入学生信息(学号、姓名、考试成绩等),由Servlet将该信息存放在JavaBeans对象中。
2)学生信息显示功能
录入完毕之后,由JSP页面显示录入的学生信息。
三、实验步骤及结果分析
(包括程序源代码及注释、程序使用/功能说明、运行结果截图——要在图中体现你的学号名字、结果分析等几部分内容。)
1)学生信息录入功能
2)学生信息显示功能
源代码
studentServlet
package com.pnkj.servlet;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.pnkj.javabean.Student;
@WebServlet(name="studentServlet", urlPatterns={"/student.do"})
public class studentServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=UTF-8");
        //获取学号、姓名、成绩
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String score = request.getParameter("score");
        System.out.println("id="+id + "name=" +name +"score="+score);
        //创建javabean对象
        Student student = new Student(id, name, score);
        //把javabean存储起来的session域
        HttpSession session = request.getSession(true);
        synchronized (session) {
            session.setAttribute("student", student);
        }
        RequestDispatcher rd = request.getRequestDispatcher("student.jsp");
        rd.forward(request, response);
//        response.sendRedirect("student.jsp");
    }
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>pnkj2018</title>
</head>
<body>
    <form action="student.do">
        <div>
            学号:<input type="text" name="id">
        </div>
        <div>
            姓名:<input type="text" name="name">
        </div>
        <div>
            考试成绩:<input type="text" name="score">
        </div>
        <input type="submit" value="登陆" class="submit">
                <input type="reset" value="取消"><br>
    </javaweb编程技术form>
</body>
</html>
student.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>pnkj2018</title>
</head>
<body>
    <jsp:useBean id="student" type="com.pnkj.javabean.Student" scope="session"></jsp:useBean>
    学号:<jsp:getProperty property="id" name="student"/>
    姓名:<jsp:getProperty property="name" name="student"/>
    成绩:<jsp:getProperty property="score" name="student"/>
</body>
</html>
Student
package com.pnkj.javabean;
public class Student {
    private String id;
    private String name;
    private String score;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getScore() {
        return score;
    }
    public void setScore(String score) {
        this.score = score;
    }
    public Student(String id, String name, String score) {
        super();
        this.id = id;
        this.name = name;
        this.score = score;
    }
    public Student() {
        super();
    }
   
}
四、实验体会
  代码要多调试才能有满意的结果