用JSP实现学生查询

  前两天学习Servlet时,用Servlet做了一个小小个学生查询功能,现在学习了JSP就用JSP来实现这个小功能。

  首先新建一个JSP文件student.jsp,代码如下:


1 <%@ page language=”java” contentType=”text/html;charset=GB2312” %>
 2 <%@ page import=”java.sql.Timestamp”%>
 3 <%@ page import=”java.text.“%>
 4 <%@ page import=”java.util.“%>
 5 <%@ page import=”com.pojo.“%>
 6 <%@ page import=”com.services.“%>
 7 
 8     
 9         
10     
11     
12         

13             出生日期:至
14             


15         

16         <%
17             String sBeginDate = request.getParameter(“begindate”);
18             String sEndDate = request.getParameter(“enddate”);
19             //将字符串转换为Timestamp
20             Timestamp beginDate = Timestamp.valueOf(“1900-1-1 00:00:00”);
21             Timestamp endDate = Timestamp.valueOf(“1900-1-1 00:00:00”);
22             try{
23                 beginDate = Timestamp.valueOf(sBeginDate+” 0:0:0”);
24                 endDate = Timestamp.valueOf(sEndDate+” 0:0:0”);
25             }catch(Exception e){
26                 e.printStackTrace();
27             }
28             StudentService ss = new StudentService();
29             //从数据库中查询结果
30             List l = ss.getStudentByDate(beginDate,endDate);
31         %>
32         <%
33             if(l.size()!= 0  && l!=null){
34         %>
35         
36
37
38
39
40
41
42
43             <%
44for(Iterator iter=l.iterator();iter.hasNext();){
45                 Student s = iter.next();
46             %>
47             
48
49
50
51
52
53
54             <%}%>
55
                              学号                 	姓名                 	出生日期                 	性别                 	家庭住址             
                                  <%=s.getSId()%>                 	<%=s.getSName()%>                 	<%=new SimpleDateFormat(“yyyy-MM-dd”).format(s.getSDate())%>                 	<%=s.getSSex()%>                 	<%=s.getSAddr()%>             
         

56         <%
57             }
58         %>
59     
60 

  再把相应的业务逻辑jar包和数据库驱动拷贝到lib目录下。

编译后的student_jsp.java代码如下:


  1 package org.apache.jsp;
  2
  3 import javax.servlet.;
  4 import javax.servlet.http.;
  5 import javax.servlet.jsp.;
  6 import java.sql.Timestamp;
  7 import java.text.;
  8 import java.util.;
  9 import com.pojo.;
 10 import com.services.*;
 11
 12 public final class student_jsp extends org.apache.jasper.runtime.HttpJspBase
 13     implements org.apache.jasper.runtime.JspSourceDependent {
 14
 15   private static final JspFactory _jspxFactory = JspFactory.getDefaultFactory();
 16
 17   private static java.util.List _jspx_dependants;
 18
 19   private javax.el.ExpressionFactory _el_expressionfactory;
 20   private org.apache.AnnotationProcessor _jsp_annotationprocessor;
 21
 22   public Object getDependants() {
 23     return _jspx_dependants;
 24   }
 25
 26   public void _jspInit() {
 27     _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
 28     _jsp_annotationprocessor = (org.apache.AnnotationProcessor) getServletConfig().getServletContext().getAttribute(org.apache.AnnotationProcessor.class.getName());
 29   }
 30
 31   public void _jspDestroy() {
 32   }
 33
 34   public void _jspService(HttpServletRequest request, HttpServletResponse response)
 35         throws java.io.IOException, ServletException {
 36
 37     PageContext pageContext = null;
 38     HttpSession session = null;
 39     ServletContext application = null;
 40     ServletConfig config = null;
 41     JspWriter out = null;
 42     Object page = this;
 43     JspWriter _jspx_out = null;
 44     PageContext _jspx_page_context = null;
 45
 46
 47     try {
 48       response.setContentType(“text/html;charset=GB2312”);
 49       pageContext = _jspxFactory.getPageContext(this, request, response,
 50                   null, true, 8192, true);
 51       _jspx_page_context = pageContext;
 52       application = pageContext.getServletContext();
 53       config = pageContext.getServletConfig();
 54       session = pageContext.getSession();
 55       out = pageContext.getOut();
 56       _jspx_out = out;
 57
 58       out.write(“\r\n”);
 59       out.write(“\r\n”);
 60       out.write(“\r\n”);
 61       out.write(“\r\n”);
 62       out.write(“\r\n”);
 63       out.write(“\r\n”);
 64       out.write(“\r\n”);
 65       out.write(“\t\r\n”);
 66       out.write(“\t\t\r\n”);
 67       out.write(“\t\r\n”);
 68       out.write(“\t\r\n”);
 69       out.write(“\t\t
\r\n”);
 70       out.write(“\t\t\t出生日期:至\r\n”);
 71       out.write(“\t\t\t\r\n”);
 72       out.write(“\t\t
\r\n”);
 73       out.write(“\t\t”);
 74
 75             String sBeginDate = request.getParameter(“begindate”);
 76             String sEndDate = request.getParameter(“enddate”);
 77             //将字符串转换为Timestamp
 78             Timestamp beginDate = Timestamp.valueOf(“1900-1-1 00:00:00”);
 79             Timestamp endDate = Timestamp.valueOf(“1900-1-1 00:00:00”);
 80             try{
 81                 beginDate = Timestamp.valueOf(sBeginDate+” 0:0:0”);
 82                 endDate = Timestamp.valueOf(sEndDate+” 0:0:0”);
 83             }catch(Exception e){
 84                 e.printStackTrace();
 85             }
 86             StudentService ss = new StudentService();
 87             //从数据库中查询结果
 88             List l = ss.getStudentByDate(beginDate,endDate);
 89
 90       out.write(“\r\n”);
 91       out.write(“\t\t”);
 92
 93             if(l.size()!= 0  && l!=null){
 94
 95       out.write(“\r\n”);
 96       out.write(“\t\t\r\n”);
 97       out.write(“\t\t\t\r\n”);
 98       out.write(“\t\t\t\t\r\n”);
 99       out.write(“\t\t\t\t\r\n”);
100       out.write(“\t\t\t\t\r\n”);
101       out.write(“\t\t\t\t\r\n”);
102       out.write(“\t\t\t\t\r\n”);
103       out.write(“\t\t\t\r\n”);
104       out.write(“\t\t\t”);
105
106for(Iterator iter=l.iterator();iter.hasNext();){
107                 Student s = iter.next();
108
109       out.write(“\r\n”);
110       out.write(“\t\t\t\r\n”);
111       out.write(“\t\t\t\t\r\n”);
114       out.write(“\t\t\t\t\r\n”);
117       out.write(“\t\t\t\t\r\n”);
120       out.write(“\t\t\t\t\r\n”);
123       out.write(“\t\t\t\t\r\n”);
126       out.write(“\t\t\t\r\n”);
127       out.write(“\t\t\t”);
128}
129       out.write(“\r\n”);
130       out.write(“\t\t
学号	姓名	出生日期	性别	家庭住址
                 “);
112       out.print(s.getSId());
113       out.write(“	“);
115       out.print(s.getSName());
116       out.write(“	“);
118       out.print(new SimpleDateFormat(“yyyy-MM-dd”).format(s.getSDate()));
119       out.write(“	“);
121       out.print(s.getSSex());
122       out.write(“	“);
124       out.print(s.getSAddr());
125       out.write(“
 
\r\n”);
131       out.write(“\t\t”);
132
133             }
134
135       out.write(“\r\n”);
136       out.write(“\t\r\n”);
137       out.write(““);
138     } catch (Throwable t) {
139       if (!(t instanceof SkipPageException)){
140         out = _jspx_out;
141         if (out != null && out.getBufferSize() != 0)
142           try { out.clearBuffer(); } catch (java.io.IOException e) {}
143         if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
144       }
145     } finally {
146       _jspxFactory.releasePageContext(_jspx_page_context);
147     }
148   }
149 }

 

相比Servlet,JSP的实现简单了许多,在JSP文件中HTML语句可以直接写,不需要拼凑。

你可能感兴趣的:(java,数据库,runtime)