一、jsp文件:
1、主页面的基本设定(index.jsp)
查看静态表格(自己创建)
<%@ include file="inner.jsp"%>
//实现静态包含文件
2、静态包含文件(inner.jsp)
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
静态包含inner页面
3、学生表单(list.jsp)
(1).表单基本样式设定
(2).导入学生信息表
<%@page import="day03_student.Student"%>
<%
List list=(List)request.getAttribute("students");
%>
(3).学生基本信息
学号
姓名
性别
年龄
<%
for(Student s:list){
%>
<%=s.getStuno() %>
<%=s.getName() %>
<%=s.getGender() %>
<%=s.getAge() %>
<%} %>
二、Java文件
1.构造学生集合(Listservlet.java)
List list=new ArrayList();
list.add(new Student(1,"stu001","张三","男",20));
list.add(new Student(2,"stu002","李四","女",21));
list.add(new Student(3,"stu003","王五","男",19));
list.add(new Student(4,"stu004","王子","女",20));
list.add(new Student(5,"stu005","天天辉","男",24));
//将数据放入request中,传递到页面
request.setAttribute("students", list);
request.getRequestDispatcher("list.jsp").forward(request, response);
2.学生实体(Student.java)
private int id;
private String stuno;
private String name;
private String gender;
private int age;
三.源代码
1.index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
My JSP 'index.jsp' starting page
查看静态表格(自己创建)
<%@ include file="inner.jsp"%>
2.inner.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
静态包含inner页面
3.list.jsp
<%@page import="day03_student.Student"%>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
My JSP 'index.jsp' starting page
<%
List list=(List)request.getAttribute("students");
%>
学号
姓名
性别
年龄
<%
for(Student s:list){
%>
<%=s.getStuno() %>
<%=s.getName() %>
<%=s.getGender() %>
<%=s.getAge() %>
<%} %>
4.Student.java
package day03_student;
/*
* 学生实体
* */
public class Student {
private int id;
private String stuno;
private String name;
private String gender;
private int age;
public Student(int id,String stuno,String name,String gender,int age){
super();
this.id=id;
this.stuno=stuno;
this.name=name;
this.gender=gender;
this.age=age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getStuno() {
return stuno;
}
public void setStuno(String stuno) {
this.stuno = stuno;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
5.Listservlet.java
package day03;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import day03_inport.StudentDao;
import day03_student.Student;//导入包
/*
* 查看全部学生
* */
public class Listservlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//构造学生集合
List list=new ArrayList();
list.add(new Student(1,"stu001","张三","男",20));
list.add(new Student(2,"stu002","李四","女",21));
list.add(new Student(3,"stu003","王五","男",19));
list.add(new Student(4,"stu004","王子","女",20));
list.add(new Student(5,"stu005","天天辉","男",24));
//将数据放入request中,传递到页面
request.setAttribute("students", list);
request.getRequestDispatcher("list.jsp").forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
public void init() throws ServletException {
// Put your code here
}
}
四、结果展示
通过浏览器打开主页面:
点击“查看学生列表”
至此实验目的基本完成
注:本实验可进一步深化为直接连接数据库进行表单的操作,这也是本人接下来基于本文发表的另一篇博客