实现网上购物系统的后台管理(增、删、改、查图书)。

实验描述:
     (实验三的任务)用户网上注册,注册信息写入数据库。
     (实验四的任务)用户登陆后,进入网上购物系统,选择书籍,加入购物车,购物完毕进入结账界面(可模拟一下,简单输出一些信息);在用户购物的期间内,可随时查看购物车的状态,进入购物车后,也可返回再选择商品;在购物期间,用户也可以注销。

    本次的任务:管理员以“admin”为用户名进行注册(或登录),对书籍进行后台管理(增、删、改
、查)。

问题:

         sql=sql+"publisher='"+publisher+"‘ where bookname='"+name+"'";与

          sql=sql+"publisher='"+publisher+"'"+" where bookname='"+name+"'";

区别很大,看似很相同,第一个不能运行,第二个oK

感受:

            很多语句和一些简单的应用很不熟,犯了很多小白问题

             练习太少了,

贴个后台代码:

                    jsp:页面

              Backmanger.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  
  <body>
   <%request.setCharacterEncoding("utf-8");
  response.setCharacterEncoding("utf-8"); %>
    <form action="backLogin.jsp" method="post">
       <table>
       <tr><td colspan="2" align="center">后台管理登陆</td></tr>
	   <tr><td>数据库名称:</td><td><input type="text" name="pdatebasename"/></td></tr>
	   <tr><td>表的名字:</td><td><input type="text" name="ptable"/></td></tr>	
	   <tr><td>用户名:</td><td><input type="text" name="puser"/></td></tr>	
	   <tr><td>密码:</td><td><input type="password" name="pwd"/></td></tr>	
	     <tr>
	     		<td  ><input type="submit" value=" 管理员登陆"/></td>
	     	    
	     </tr>  
       </table>
    </form>
  </body>
</html>


logIn.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="dao.backConnect"  %>
<%@ page import="bean.backConnectbean" %>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'backLogin.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
       <%request.setCharacterEncoding("utf-8");
  response.setCharacterEncoding("utf-8"); %>
  <jsp:useBean id="user1" class="bean.backConnectbean" scope="session"></jsp:useBean>

  <jsp:setProperty property="*" name="user1"/>
 
  <%
 // 	System.out.println(user.getPdatebasename()+"  "+user.getPuser()+" "+user.getPwd());
  	
   // String ps=request.getParameter("pwd");
     boolean flag=(new backConnect()).getConn(user1.getPdatebasename(),user1.getPuser(),user1.getPwd());
      System.out.println("flag  初始化成功");
 
      if(flag)
      {
         response.sendRedirect("displayBook1.jsp");
      }
      else
      {
         response.sendRedirect("BackManger.jsp");
      }
   %>
  
  </body>
</html>
display
   <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="dao.BookDao"%>
<%@page import="bean.BookBean"%>
<%

String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'displayBooks.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
         <%request.setCharacterEncoding("utf-8");
  response.setCharacterEncoding("utf-8"); %>
   <%
  
    %>
  欢迎您 管理员,请选择相关操作
 <form action="process.jsp" method="post">"
  <table border="1">
 <tr><td>请选择</td>
 <td>书名</td>
 <td>出版社</td>
 <td>作者</td>
 <td>价格</td>
 <td>出版日期</td>
 <td>库存数量</td>
 </tr>
  <%
    BookDao bookDao=new BookDao();
    ArrayList list=bookDao.selectAllBooks();
    Iterator it=list.iterator();
    while(it.hasNext())
    {
       BookBean book=(BookBean)it.next();
    %>
    <tr>
    <td><input type="checkbox"  name="bookid" value="<%=book.getBookid() %>"/> </td>
    <td><%=book.getBookname() %></td>
    <td><%=book.getPublisher() %></td>
    <td><%=book.getAuthor() %></td>
    <td><%=book.getPrice() %></td>
    <td><%=book.getPublishDate() %></td>
    <td><%=book.getCount() %></td>
    </tr>
    <%   
    }
   %>
   <tr>
   <td>
   <select name="flag">
<option value="增加" selected>增加</option>
<option value="减少">减少</option>
<option value="改正">改正</option>
<option value="查询">查询</option>
</select><br>
   </inut>
   </td>
   </tr>
   <tr><td colspan="7" align="center">
   <input type="submit" value="提交"/>
   </td></tr>
    </table></form>
  </body>
</html>

process.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'process.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
     <%
     request.setCharacterEncoding("utf-8");
     response.setCharacterEncoding("utf-8"); 
     %>
   <%
      String flag=request.getParameter("flag");
      
      if(flag.equals("增加"))
      {
       response.sendRedirect("zhengjia.jsp");
      }
      else if(flag.equals("减少"))
      {
      response.sendRedirect("jianshao.jsp");
      }else if(flag.equals("改正"))
      {
      response.sendRedirect("gaizheng.jsp");
      }
      else  if(flag.equals("查询"))
      {
      response.sendRedirect("chaxun.jsp");
      }
    %>
   
  </body>
</html>
zhengjia.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'regist.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
   <%request.setCharacterEncoding("utf-8");
   response.setCharacterEncoding("utf-8"); %>
  <form action="zhengjiaservlet" method="get">
     <table>
       <tr><td colspan="2" align="center">要增加的内容</td></tr>
       <tr><td>编号:</td><td><input type="text" name="bookid"/></td></tr>
	   <tr><td>书名:</td><td><input type="text" name="bookname"/></td></tr>
	   <tr><td>出版社:</td><td><input type="text" name="publisher"/></td></tr>	
	   <tr><td>作者:</td><td><input type="text" name="author" /> 
	   
	   <tr><td>价格:</td><td><input type="text" name="price"/></td></tr>	
	   <tr><td>出版日期:</td><td><input type="text" name="publishDate"/></td></tr>
	   <tr><td>数量:</td><td><input type="text" name="count"/></td></tr>   
	     <tr><td colspan="2" align="center"><input type="submit" value="提交"/></td></tr>   
       </table></form>



  </body>
</html>
jianshao.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'regist.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
   <%request.setCharacterEncoding("utf-8");
   response.setCharacterEncoding("utf-8"); %>
  <form action="jianshaoservlet" method="get">
     <table>
       <tr><td colspan="2" align="center">要删去的书名</td></tr>
      
	   <tr><td>书名:</td><td><input type="text" name="bookname"/></td></tr>
	  
	     <tr><td colspan="2" align="center"><input type="submit" value="提交"/></td></tr>   
       </table></form>



  </body>
</html>

gaizheng.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'regist.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
   <%request.setCharacterEncoding("utf-8");
   response.setCharacterEncoding("utf-8"); %>
  <form action="gaizheng" method="get">
     <table>
       <tr><td colspan="2" align="center">要改正的书名及要修改的相关信息</td></tr>
      
	   <tr><td>书名:</td><td><input type="text" name="bookname"/></td></tr>
	  <tr><td>出版社:</td><td><input type="text" name="publisher"/></td></tr>	
	   <tr><td>作者:</td><td><input type="text" name="author" /> 
	   <tr><td>价格:</td><td><input type="text" name="price"/></td></tr>	
	   <tr><td>出版日期:</td><td><input type="text" name="publishDate"/></td></tr>
	   <tr><td>数量:</td><td><input type="text" name="count"/></td></tr>   
	     <tr><td colspan="2" align="center"><input type="submit" value="提交"/></td></tr>   
       </table></form>



  </body>
</html>

chaxun.jsp (水水滴)

   <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="dao.BookDao"%>
<%@page import="bean.BookBean"%>
<%

String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'displayBooks.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
         <%request.setCharacterEncoding("utf-8");
  response.setCharacterEncoding("utf-8"); %>
   <%
  
    %>
  <p>书籍信息</p>
 <form action="process.jsp" method="post">"
  <table border="1">
 <tr><td>编号</td>
 <td>书名</td>
 <td>出版社</td>
 <td>作者</td>
 <td>价格</td>
 <td>出版日期</td>
 <td>库存数量</td>
 </tr>
  <%
    BookDao bookDao=new BookDao();
    ArrayList list=bookDao.selectAllBooks();
    Iterator it=list.iterator();
    while(it.hasNext())
    {
       BookBean book=(BookBean)it.next();
    %>
    <tr>
    <td><input type="checkbox"  name="bookid" value="<%=book.getBookid() %>"/> </td>
    <td><%=book.getBookname() %></td>
    <td><%=book.getPublisher() %></td>
    <td><%=book.getAuthor() %></td>
    <td><%=book.getPrice() %></td>
    <td><%=book.getPublishDate() %></td>
    <td><%=book.getCount() %></td>
    </tr>
    <%   
    }
   %>
   <tr>
   <td>

   </inut>
   </td>
   </tr>

   </td></tr>
    </table></form>
  </body>
</html>
bean

backbean.

package dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class backConnect {
	
	  public boolean getConn(String datebaseName,String user,String pwd)
	  {
		  
		  try {
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
			System.out.print(datebaseName+"  "+user+"  "+pwd);
			String url="jdbc:sqlserver://127.0.0.1:1433;databaseName="+datebaseName;
			  Connection con;
			con=DriverManager.getConnection(url,user,pwd);
			System.out.print("连接成功");
			return true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			System.out.print("连接失败一");
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			System.out.print("连接失败二");
			e.printStackTrace();
		}
		return false;
	  }
}




dao 里的

backconnect.java


package bean;

public class backConnectbean {
	private String pdatebasename="nothing";
	private String ptable="nothing";
	private String puser="nothing";
	private String pwd="nothing";

	public String getPdatebasename() {
		return pdatebasename;
	}
	public void setPdatebasename(String datebaseName) {
		this.pdatebasename = datebaseName;
	}
	public String getPtable() {
		return ptable;
	}
	public void setPtable(String ptable) {
		this.ptable = ptable;
	}
	public String getPuser() {
		return puser;
	}
	public void setPuser(String puser) {
		this.puser = puser;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	
}
巨关键的bookdao.java

package dao;

import dao.BaseDao;
import java.io.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import bean.BookBean;
import java.sql.*;
public class BookDao {
	Connection con=new BaseDao().getConn();
	private PreparedStatement pstat;
	String sql="";
	public boolean Zhengjia(BookBean user)
	{
		
		sql = "insert into book values(?,?,?,?,?,?,?)";
	   try
	   {
		pstat = con.prepareStatement(sql);
		pstat.setString(1,user.getBookid());
		pstat.setString(2,user.getBookname());
		pstat.setString(3,user.getPublisher());
		pstat.setString(4,user.getAuthor());
		pstat.setString(5,user.getPrice());
		pstat.setString(6,user.getPublishDate());
		
		pstat.setInt(7,user.getCount());
		pstat.executeUpdate();
		pstat.close();
		con.close();
	    return true;
	    }
	   catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	   return false;
	}
	public boolean jianshao(String name)
	{
	      sql = "delete from  book where bookname='"+name+"'";
		  try
		   {
			Statement sql1;
			sql1=con.createStatement();
			sql1.executeUpdate(sql);
			
			con.close();
		    return true;
		    }
		   catch (SQLException e) {
				// TODO Auto-generated catch block
				//e.printStackTrace();
			}

	   return false;
	}
	public boolean gaizheng(String name,String publisher,String author,String price,String publishDate,int count)
	{
		if(true)
		{

			System.out.println(name+publisher+author+price+publishDate+count);
	
		}
		
	      String  sql = "update  book set ";
	      String  sql1 = "update  book set ";
	      String  sql2 = "update  book set ";
	      String  sql3 = "update  book set ";
	      String  sql4 = "update  book set ";
	      

	      if(publisher!=null)
	      {
	    	  sql=sql+"publisher='"+publisher+"'"+" where bookname='"+name+"'";
	      }
	      if(author!=null)
	      {
	    	  sql1+="author='"+author+"'"+" where bookname='"+name+"'";
	      }
	      if(price!=null)
	      {
	    	  sql2+="price='"+price+"'"+" where bookname='"+name+"'";
	      }
	      if(publishDate!=null)
	      {
	    	  sql3+="poublishDate='"+publishDate+"'"+" where bookname='"+name+"'";
	      }
	      if(count!=0 )
	      {
	    	  sql4+="count='"+count+"'"+" where bookname='"+name+"'";;
	      }
	       
	      System.out.println(sql);
	      System.out.println("try 前");
		  try
		   {
			Statement sql11;
			sql11=con.createStatement();
			sql11.executeUpdate(sql);
			System.out.println("try 中");
			sql11.executeUpdate(sql1);
			sql11.executeUpdate(sql2);
			sql11.executeUpdate(sql3);
			sql11.executeUpdate(sql4);
	
			con.close();
			System.out.println("try 后");
		    return true;
		    }
		   catch (SQLException e) {
				// TODO Auto-generated catch block
				//e.printStackTrace();
			}

	   return false;
	}
   public ArrayList selectAllBooks()
   {
	   ArrayList<BookBean> list=new ArrayList();
	   String selectSql="select * from book";
	   try {
		PreparedStatement pst=con.prepareStatement(selectSql);
		ResultSet rs=pst.executeQuery();
		while(rs.next())
		{
			BookBean book=new BookBean();
			book.setBookid(rs.getString(1));
			book.setBookname(rs.getString(2));
			book.setPublisher(rs.getString(3));
			book.setAuthor(rs.getString(4));
			book.setPrice(rs.getString(5));
			book.setPublishDate(rs.getString(6));
			book.setCount(rs.getInt(7));
			list.add(book);
		}
		
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	   
	   
	   return list;
   }
   
   public String selectBookById(String bookId)
   {
	   String bookName=null;
	   String selectSql="select bookname from book where bookid=?";
	   try {
		PreparedStatement pst=con.prepareStatement(selectSql);
		pst.setString(1, bookId);
		ResultSet rs=pst.executeQuery();
		if(rs.next())
		{
		    bookName=rs.getString(1);
		}
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return bookName;
	
   }
}


servlet

zhengjia.jsp

package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.BookDao;

public class gaizheng extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public gaizheng() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		request.setCharacterEncoding("utf-8");
		String bookname = new String(request.getParameter("bookname").getBytes("ISO8859_1"),"utf-8");
		String publisher = new String(request.getParameter("publisher").getBytes("ISO8859_1"),"utf-8");
		String author = new String(request.getParameter("author").getBytes("ISO8859_1"),"utf-8");
		String price = new String(request.getParameter("price").getBytes("ISO8859_1"),"utf-8");
		String publishDate = new String(request.getParameter("publishDate").getBytes("ISO8859_1"),"utf-8");
		String count1 = new String(request.getParameter("count").getBytes("ISO8859_1"),"utf-8");
		int count=Integer.parseInt(count1);

		BookDao dao = new BookDao();
		
		if(dao.gaizheng(bookname,publisher,author,price,publishDate,count))
		{
			response.setContentType("text/html");
			PrintWriter out = response.getWriter();
			out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
			out.println("<HTML>");
			out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
			out.println("  <BODY>");
			out.print(" Successful!!! ");
		
			out.println("  </BODY>");
			out.println("</HTML>");
			out.flush();
			out.close();
		}
		else
		{
			
			response.setContentType("text/html");
			PrintWriter out = response.getWriter();
			out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
			out.println("<HTML>");
			out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
			out.println("  <BODY>");
			out.print(" Defeated!!! ");
		
			out.println("  </BODY>");
			out.println("</HTML>");
			out.flush();
			out.close();
		}

	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out
				.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the POST method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}

jianshao.jsp

package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.BookDao;

public class jianshaoservlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public jianshaoservlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		request.setCharacterEncoding("utf-8");
		String bookname = new String(request.getParameter("bookname").getBytes("ISO8859_1"),"utf-8");
		BookDao dao = new BookDao();
		
		if(dao.jianshao(bookname))
		{
			response.setContentType("text/html");
			PrintWriter out = response.getWriter();
			out
					.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
			out.println("<HTML>");
			out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
			out.println("  <BODY>");
			out.print(" Successful!!! ");
		
			out.println("  </BODY>");
			out.println("</HTML>");
			out.flush();
			out.close();
		}
		else
		{
			
			response.setContentType("text/html");
			PrintWriter out = response.getWriter();
			out
					.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
			out.println("<HTML>");
			out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
			out.println("  <BODY>");
			out.print(" Defeated!!! ");
		
			out.println("  </BODY>");
			out.println("</HTML>");
			out.flush();
			out.close();
		}

	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out
				.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the POST method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}

gaizheng.jsp

package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.BookDao;

public class gaizheng extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public gaizheng() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		request.setCharacterEncoding("utf-8");
		String bookname = new String(request.getParameter("bookname").getBytes("ISO8859_1"),"utf-8");
		String publisher = new String(request.getParameter("publisher").getBytes("ISO8859_1"),"utf-8");
		String author = new String(request.getParameter("author").getBytes("ISO8859_1"),"utf-8");
		String price = new String(request.getParameter("price").getBytes("ISO8859_1"),"utf-8");
		String publishDate = new String(request.getParameter("publishDate").getBytes("ISO8859_1"),"utf-8");
		String count1 = new String(request.getParameter("count").getBytes("ISO8859_1"),"utf-8");
		int count=Integer.parseInt(count1);

		BookDao dao = new BookDao();
		
		if(dao.gaizheng(bookname,publisher,author,price,publishDate,count))
		{
			response.setContentType("text/html");
			PrintWriter out = response.getWriter();
			out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
			out.println("<HTML>");
			out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
			out.println("  <BODY>");
			out.print(" Successful!!! ");
		
			out.println("  </BODY>");
			out.println("</HTML>");
			out.flush();
			out.close();
		}
		else
		{
			
			response.setContentType("text/html");
			PrintWriter out = response.getWriter();
			out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
			out.println("<HTML>");
			out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
			out.println("  <BODY>");
			out.print(" Defeated!!! ");
		
			out.println("  </BODY>");
			out.println("</HTML>");
			out.flush();
			out.close();
		}

	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out
				.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the POST method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}

代码打包:

去我的资源里下吧

这个链接是城通网盘 的链接

http://home.400gb.com/mydisk.php?item=files&action=index&folder_id=1213171



  • 1
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值