zoukankan      html  css  js  c++  java
  • 用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 <html>
     8     <head>
     9         <title>学生信息</title>
    10     </head>
    11     <body>
    12         <form action="student.jsp" method="GET">
    13             出生日期:<input type="text" name="begindate">至<input type="text" name="enddate">
    14             <input type="submit" value="查询">
    15         </form>
    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<Student> l = ss.getStudentByDate(beginDate,endDate); 
    31         %>
    32         <%
    33             if(l.size()!= 0  && l!=null){
    34         %>
    35         <table border="1">
    36             <tr>
    37                 <td>学号</td>
    38                 <td>姓名</td>
    39                 <td>出生日期</td>
    40                 <td>性别</td>
    41                 <td>家庭住址</td>
    42             </tr>
    43             <%
    44                 for(Iterator<Student> iter=l.iterator();iter.hasNext();){
    45                 Student s = iter.next();
    46             %>
    47             <tr>
    48                 <td><%=s.getSId()%></td>
    49                 <td><%=s.getSName()%></td>
    50                 <td><%=new SimpleDateFormat("yyyy-MM-dd").format(s.getSDate())%></td>
    51                 <td><%=s.getSSex()%></td>
    52                 <td><%=s.getSAddr()%></td>
    53             </tr>
    54             <%}%>
    55         </table>
    56         <%
    57             }
    58         %>
    59     </body>
    60 </html>

      再把相应的业务逻辑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("<html>\r\n");
     65       out.write("\t<head>\r\n");
     66       out.write("\t\t<title>学生信息</title>\r\n");
     67       out.write("\t</head>\r\n");
     68       out.write("\t<body>\r\n");
     69       out.write("\t\t<form action=\"student.jsp\" method=\"GET\">\r\n");
     70       out.write("\t\t\t出生日期:<input type=\"text\" name=\"begindate\">至<input type=\"text\" name=\"enddate\">\r\n");
     71       out.write("\t\t\t<input type=\"submit\" value=\"查询\">\r\n");
     72       out.write("\t\t</form>\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<Student> 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<table border=\"1\">\r\n");
     97       out.write("\t\t\t<tr>\r\n");
     98       out.write("\t\t\t\t<td>学号</td>\r\n");
     99       out.write("\t\t\t\t<td>姓名</td>\r\n");
    100       out.write("\t\t\t\t<td>出生日期</td>\r\n");
    101       out.write("\t\t\t\t<td>性别</td>\r\n");
    102       out.write("\t\t\t\t<td>家庭住址</td>\r\n");
    103       out.write("\t\t\t</tr>\r\n");
    104       out.write("\t\t\t");
    105 
    106                 for(Iterator<Student> iter=l.iterator();iter.hasNext();){
    107                 Student s = iter.next();
    108             
    109       out.write("\r\n");
    110       out.write("\t\t\t<tr>\r\n");
    111       out.write("\t\t\t\t<td>");
    112       out.print(s.getSId());
    113       out.write("</td>\r\n");
    114       out.write("\t\t\t\t<td>");
    115       out.print(s.getSName());
    116       out.write("</td>\r\n");
    117       out.write("\t\t\t\t<td>");
    118       out.print(new SimpleDateFormat("yyyy-MM-dd").format(s.getSDate()));
    119       out.write("</td>\r\n");
    120       out.write("\t\t\t\t<td>");
    121       out.print(s.getSSex());
    122       out.write("</td>\r\n");
    123       out.write("\t\t\t\t<td>");
    124       out.print(s.getSAddr());
    125       out.write("</td>\r\n");
    126       out.write("\t\t\t</tr>\r\n");
    127       out.write("\t\t\t");
    128 }
    129       out.write("\r\n");
    130       out.write("\t\t</table>\r\n");
    131       out.write("\t\t");
    132 
    133             }
    134         
    135       out.write("\r\n");
    136       out.write("\t</body>\r\n");
    137       out.write("</html>");
    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语句可以直接写,不需要拼凑。

  • 相关阅读:
    Spring Cloud Hystrix(熔断器)介绍及使用
    Fign入门学习
    Ribbon入门学习
    浅谈JSON5
    JSONObject 与 JSON 互转
    Windows下安装使用jmeter
    MongoDB 数据库高级进阶
    Mysq-l数据库下载以及安装(至安装成功cmd可访问)
    Mysql-数据库下载
    端口占用
  • 原文地址:https://www.cnblogs.com/liuling/p/JSP2.html
Copyright © 2011-2022 走看看