J2EE Servlet 学习笔记2
2014-04-11 18:22
369 查看
<?xml version="1.0" encoding="gb2312"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <context-param> <param-name>driverClass</param-name> <param-value>com.mysql.jdbc.Driver</param-value> </context-param> <context-param> <param-name>url</param-name> <param-value>jdbc:mysql://10.33.52.12:3306/bookstore</param-value> </context-param> <context-param> <param-name>user</param-name> <param-value>intelFC</param-value> </context-param> <context-param> <param-name>password</param-name> <param-value>qHcqSLnH4T3T9PRj</param-value> </context-param> <servlet> <servlet-name>CreateDBServlet</servlet-name> <servlet-class>org.laotou99.abc3.servlet.CreateDBServlet</servlet-class> <init-param> <param-name>driverClass</param-name> <param-value>com.mysql.jdbc.Driver</param-value> </init-param> <init-param> <param-name>url</param-name> <param-value>jdbc:mysql://10.33.52.12:3306</param-value> </init-param> <init-param> <param-name>user</param-name> <param-value>intelFC</param-value> </init-param> <init-param> <param-name>password</param-name> <param-value>qHcqSLnH4T3T9PRj</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>CreateDBServlet</servlet-name> <url-pattern>/createdb</url-pattern> </servlet-mapping> <servlet> <servlet-name>ListServlet</servlet-name> <servlet-class>org.laotou99.abc3.servlet.ListServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>ListServlet</servlet-name> <url-pattern>/list</url-pattern> </servlet-mapping> <servlet> <servlet-name>CreateAccountServlet</servlet-name> <servlet-class>org.laotou99.abc3.servlet.CreateAccountServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CreateAccountServlet</servlet-name> <url-pattern>/account</url-pattern> </servlet-mapping> </web-app>
<html> <head> <title>网上书店</title> <script language="javaScript"> <!-- function fsubmit() { if(searchForm.rcond[0].checked) { searchForm.action="list?cond=all" } else if(searchForm.rcond[1].checked) { searchForm.action="list?cond=precision" } else { searchForm.action="list?cond=keyword" } } function hideall() { if(searchForm.rcond[0].checked) { pre.style.display = "none"; key.style.display = "none"; } } function showpre() { if(searchForm.rcond[1].checked) { pre.style.display = ""; key.style.display = "none"; } else { pre.style.display = "none"; } } function showkey() { if(searchForm.rcond[2].checked) { key.style.display = ""; pre.style.display = "none"; } else { key.style.display = "none"; } } --> </script> </head> <body> <form name="searchForm" action="" method="post" onClick="fsubmit()"> <input type="radio" name="rcond" onclick="hideall()"> 查看所有图书<p> <input type="radio" name="rcond" onclick="showpre()"> 精准搜索<p> <table id=pre style="DISPLAY: none"> <tr> <td>书名:</td> <td><input type="text" name="title"></td> </tr> <tr> <td>作者:</td> <td><input type="text" name="author"></td> </tr> <tr> <td>出版社:</td> <td><input type="text" name="bookconcern"></td> </tr> </table><p> <input type="radio" name="rcond" onclick="showkey()"> 关键字搜索<p> <table id=key style="DISPLAY: none"> <tr> <td>请输入关键字:</td> <td><input type="text" name="keyword"></td> </tr> </table><p> <input type="reset" value="重置"> <input type="submit" value="搜索"> </form> </body> </html>
package org.laotou99.abc3.servlet; import javax.servlet.*; import java.io.*; import javax.servlet.http.*; import java.sql.*; //javac -d ..\WEB-INF\classes ListServlet.java public class ListServlet extends HttpServlet { private String url; private String user; private String password; public void init() throws ServletException { ServletContext sc = getServletContext(); String driverClass = sc.getInitParameter("driverClass"); url = sc.getInitParameter("url"); user = sc.getInitParameter("user"); password = sc.getInitParameter("password"); try{ Class.forName(driverClass); }catch(ClassNotFoundException ce){ throw new ServletException("加载数据库驱动失败"); } } public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { Connection conn = null; Statement stmt = null; ResultSet rs=null; req.setCharacterEncoding("gb2312"); String condition=req.getParameter("cond"); if(null==condition || condition.equals("")) { resp.sendRedirect("search.html"); return; } resp.setContentType("text/html;charset=gb2312"); PrintWriter out = resp.getWriter(); try{ conn=DriverManager.getConnection(url,user,password); stmt=conn.createStatement(); if(condition.equals("all")) { rs=stmt.executeQuery("select * from bookinfo"); printBookInfo(out,rs); out.close(); } else if(condition.equals("precision")) { String title = req.getParameter("title"); String author = req.getParameter("author"); String bookconcern = req.getParameter("bookconcern"); if((null==title || title.equals("")) && (null==author || title.equals("")) && (null==bookconcern || title.equals(""))) { resp.sendRedirect("search.html"); return; } StringBuffer sb = new StringBuffer("select * from bookinfo where "); boolean bFlag = false; if(!title.equals("")) { sb.append("title = "+"'"+title+"'"); bFlag=true; } if(!author.equals("")) { if(bFlag) sb.append("and author = "+"'"+author+"'"); else { sb.append("author = "+"'"+author+"'"); bFlag=true; } } if(!bookconcern.equals("")) { if(bFlag) sb.append("and bookconcern = "+"'"+bookconcern+"'"); else { sb.append("bookconcern = "+"'"+bookconcern+"'"); } } rs = stmt.executeQuery(sb.toString()); printBookInfo(out,rs); out.close(); } else if(condition.equals("keyword")) { String keyword = req.getParameter("keyword"); if(null==keyword || keyword.equals("")) { resp.sendRedirect("search.html"); return; } String strSQL = "select * from bookinfo where title like'%"+keyword+"%'"; rs = stmt.executeQuery(strSQL); printBookInfo(out,rs); out.close(); } else { resp.sendRedirect("search.html"); return; } }catch(SQLException se){ se.printStackTrace(); } finally { if(rs!=null){ try { rs.close(); } catch(SQLException se) { se.printStackTrace(); } rs=null; } if(stmt!=null) { try { stmt.close(); } catch(SQLException se) { se.printStackTrace(); } stmt=null; } if(conn!=null) { try { conn.close(); } catch(SQLException se) { se.printStackTrace(); } conn=null; } } } public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req,resp); } private void printBookInfo(PrintWriter out,ResultSet rs) { out.println("<html><head>"); out.println("<title>图书信息</title>"); out.println("</head><body>"); out.println("<table border=1><caption>图书信息</caption>"); out.println("<tr><th>书名</th><th>作者</th><th>出版社</th><th>价格</th><th>发行日期</th></tr>"); try{ while(rs.next()) { out.println("<tr>"); out.println("<td>"+rs.getString("title")+"</td>"); out.println("<td>"+rs.getString("author")+"</td>"); out.println("<td>"+rs.getString("bookconcern")+"</td>"); out.println("<td>"+rs.getFloat("price")+"</td>"); out.println("<td>"+rs.getDate("publish_date")+"</td>"); out.println("</tr>"); } }catch(SQLException se){ se.printStackTrace(); }finally { if(rs!=null){ try { rs.close(); } catch(SQLException se) { se.printStackTrace(); } rs=null; } } out.println("</table></body></html>"); out.println(""); } }
package org.laotou99.abc3.servlet; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; //import com.mysql.jdbc.Driver; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; //javac -d ..\WEB-INF\classes CreateDBServlet.java public class CreateDBServlet extends HttpServlet { private String url; private String user; private String password; public void init() throws ServletException { String driverClass = getInitParameter("driverClass"); url=getInitParameter("url"); user=getInitParameter("user"); password=getInitParameter("password"); try{ Class.forName(driverClass); }catch(ClassNotFoundException ce){ throw new ServletException("加载数据库驱动失败"); } } public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { Connection conn = null; Statement stmt = null; try{ conn=DriverManager.getConnection(url,user,password); stmt=conn.createStatement(); stmt.executeUpdate("drop database if exists bookstore"); stmt.executeUpdate("create database bookstore DEFAULT CHARACTER SET gb2312 COLLATE gb2312_bin"); stmt.executeUpdate("use bookstore"); stmt.executeUpdate("create table bookinfo("+ "id INT not null primary key, "+ "title VARCHAR(50) CHARACTER SET gb2312 COLLATE gb2312_bin not null, "+ "author VARCHAR(50) not null, "+ "bookconcern VARCHAR(100) not null, "+ "publish_date DATE not null, "+ "price FLOAT(4,2) not null, "+ "amount SMALLINT, "+ "remark VARCHAR(200))"+ "ENGINE=InnoDB CHARACTER SET gb2312 COLLATE gb2312_bin"); stmt.addBatch("insert into bookinfo values("+ "2,"+ "'Java WEB KKKK',"+ "'abc',"+ "'bbb',"+ "'2014-04-11',"+ "99.00,"+ "35,"+ "null)"); stmt.addBatch("insert into bookinfo values("+ "3,"+ "'Struts 2 KKKK ',"+ "'abc',"+ "'bbb',"+ "'2014-04-11',"+ "79.00,"+ "33,"+ "null)"); stmt.addBatch("insert into bookinfo values("+ "1,"+ "'ServletJsp KKKK ',"+ "'abc',"+ "'bbb',"+ "'2014-04-11',"+ "88.00,"+ "10,"+ "null)"); stmt.executeBatch(); resp.setContentType("text/html;charset=GBK"); PrintWriter out = resp.getWriter(); out.println("数据库创建成功"); out.close(); }catch(SQLException se){ throw new ServletException(se); }finally{ if(stmt!=null){ try{ stmt.close(); }catch(SQLException se){ se.printStackTrace(); } stmt=null; } if(conn!=null){ try{ conn.close(); }catch(SQLException se){ se.printStackTrace(); } conn=null; } } } }
package org.laotou99.abc3.servlet; import javax.servlet.*; import java.io.*; import javax.servlet.http.*; import java.sql.*; //javac -d ..\WEB-INF\classes CreateAccountServlet.java public class CreateAccountServlet extends HttpServlet { private String url; private String user; private String password; public void init() throws ServletException { ServletContext sc = getServletContext(); String driverClass = sc.getInitParameter("driverClass"); url = sc.getInitParameter("url"); user = sc.getInitParameter("user"); password = sc.getInitParameter("password"); try{ Class.forName(driverClass); }catch(ClassNotFoundException ce){ throw new ServletException("加载数据库驱动失败"); } } public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { Connection conn = null; Statement stmt = null; PreparedStatement pstmt = null; try{ conn = DriverManager.getConnection(url,user,password); stmt=conn.createStatement(); stmt.executeUpdate("create table account("+ "userid VARCHAR(10) CHARACTER SET gb2312 COLLATE gb2312_bin not null primary key, "+ "balance FLOAT(6,2))"+ "ENGINE = InnoDB CHARACTER SET gb2312 COLLATE gb2312_bin"); pstmt=conn.prepareStatement("insert into account values(?,?)"); pstmt.setString(1,"Jia"); pstmt.setFloat(2,2000.00f); pstmt.executeUpdate(); pstmt.setString(1,"Yi"); pstmt.setFloat(2,500.00f); pstmt.executeUpdate(); resp.setContentType("text/html;charset=GBK"); PrintWriter out = resp.getWriter(); out.println("创建account表成功"); out.close(); }catch(SQLException se){ se.printStackTrace(); }finally{ if(stmt!=null){ try{ stmt.close(); }catch(SQLException se){ se.printStackTrace(); } stmt=null; } if(pstmt!=null){ try{ pstmt.close(); }catch(SQLException se){ se.printStackTrace(); } pstmt=null; } if(conn!=null){ try{ conn.close(); }catch(SQLException se){ se.printStackTrace(); } conn=null; } } } }
相关文章推荐
- J2EE Servlet 学习笔记5
- J2EE Servlet 学习笔记3
- J2EE与中间件 学习笔记2 Web组件 Servlet过滤器 监听器
- J2EE学习笔记(五)之Servlet中的Filter过滤器
- 初学 j2ee servlet 学习笔记(1)
- J2EE与中间件 学习笔记1 JDBC Session Servlet
- servlet学习笔记一 J2EE概念
- 手动创建Servlet--J2EE学习笔记
- 【J2EE核心开发学习笔记 005】Servlet开发
- J2EE Servlet 学习笔记
- Servlet笔记一之J2EE基本学习路线
- J2EE Servlet 学习笔记4
- Spring Boot 学习笔记(五) 配置Servlet容器
- Servlet入门学习笔记
- Servlet与Jsp学习笔记--3、Cookie & jsp
- Java Web学习笔记——Servlet
- Servlet学习笔记五——另外的方法创建一个数据库
- Servlet学习笔记_Servlet
- [HeadFirst-JSPServlet学习笔记][第三章:实战MVC]
- JSP&Servlet学习笔记(一)