按学生的出生年龄段查询(输入起止日期,查询出来的结果显示在表格里)
首先在myeclipse下把做好的web项目打成exam.jar包,再配置环境变量classpath,在后面加上该exam.jar包的路径(这一步很重要,否则编译时会报错,很多类找不到)。然后把exam.jar拷贝到tomcat的lib目录下,将数据库驱动文件sqljdbc.jar也拷贝到该目录下。
新建一个StudentServlet.java,代码如下:
1 import java.io.*; 2 import java.util.*; 3 import java.sql.Timestamp; 4 import java.text.*; 5 import com.pojo.*; 6 import com.services.*; 7 import javax.servlet.*; 8 import javax.servlet.http.*; 9 public class StudentServlet extends HttpServlet{ 10 public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{ 11 doPost(request,response); //如果是GET提交就调用doPost()来执行 12 } 13 public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{ 14 String sBeginDate = request.getParameter("beginDate"); 15 String sEndDate = request.getParameter("endDate"); 16 //将字符串转换为Timestamp 17 Timestamp beginDate = Timestamp.valueOf(sBeginDate+" 0:0:0"); 18 Timestamp endDate = Timestamp.valueOf(sEndDate+" 0:0:0"); 19 StudentService ss = new StudentService(); 20 //从数据库中查询结果 21 List<Student> l = ss.getStudentByDate(beginDate,endDate); 22 //用StringBuffer来存储返回给客户端的html标签 23 StringBuffer sbHtml = new StringBuffer(); 24 sbHtml.append("<html>"); 25 sbHtml.append("<head>"); 26 sbHtml.append("<title>学生查询</title>"); 27 sbHtml.append("</head>"); 28 sbHtml.append("<body>"); 29 sbHtml.append("<table border=1>"); 30 sbHtml.append("<tr>"); 31 sbHtml.append("<td>学号</td>"); 32 sbHtml.append("<td>姓名</td>"); 33 sbHtml.append("<td>出生日期</td>"); 34 sbHtml.append("<td>性别</td>"); 35 sbHtml.append("<td>家庭住址</td>"); 36 sbHtml.append("</tr>"); 37 for(Iterator<Student> iter=l.iterator();iter.hasNext();){ 38 Student s = iter.next(); 39 sbHtml.append("<tr>"); 40 sbHtml.append("<td>"); 41 sbHtml.append(s.getSId()); 42 sbHtml.append("</td>"); 43 sbHtml.append("<td>"); 44 sbHtml.append(s.getSName()); 45 sbHtml.append("</td>"); 46 sbHtml.append("<td>"); 47 sbHtml.append(new SimpleDateFormat("yyyy-MM-dd").format(s.getSDate())); 48 sbHtml.append("</td>"); 49 sbHtml.append("<td>"); 50 sbHtml.append(s.getSSex()); 51 sbHtml.append("</td>"); 52 sbHtml.append("<td>"); 53 sbHtml.append(s.getSAddr()); 54 sbHtml.append("</td>"); 55 sbHtml.append("</tr>"); 56 } 57 sbHtml.append("</table>"); 58 sbHtml.append("</body>"); 59 sbHtml.append("</html>"); 60 response.setContentType("text/html;charset=gb2312"); 61 response.getWriter().println(sbHtml); 62 } 63 }
编译StudentServlet.java,将生成的class文件拷贝到classes目录里面。
配置web.xml,在该文件加上如下代码:
1 <servlet> 2 <servlet-name>Myservlet1</servlet-name> 3 <servlet-class>StudentServlet</servlet-class> 4 </servlet> 5 <servlet-mapping> 6 <servlet-name>Myservlet1</servlet-name> 7 <url-pattern>/StudentServlet</url-pattern> 8 </servlet-mapping>
然后再新建student.html文件:
1 <html> 2 <head> 3 <title>学生管理</title> 4 </head> 5 <body> 6 <h1>根据学生出生日期段查询</h1> 7 <form action="StudentServlet" method="post"> 8 出生日期:<input type="text" name="beginDate">至<input type="text" name="endDate"> 9 <input type="submit" name=submit value="查询学生"> 10 </form> 11 </body> 12 </html>