程序员人生 网站导航

jsp如何使用javabean

栏目:htmlcss时间:2015-02-13 08:22:53

javabean1般放在src目录下

jsp文件1般放在webroot下

src和webroot在同1个目录下

下面是我的目录结构,附带jsp代码和javabean代码


jsp代码

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ page import="java.sql.*" %> 
<%
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 'AddToMyFavorite.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  bgcolor="c0ffc0">
  <jsp:useBean id="DB" scope="page" class="lytjb.DB"></jsp:useBean>
    <%
    String username= (String)session.getAttribute("username_session");//获得当前用户
    String bookid=request.getParameter("bookid");//获得收藏书籍的id
    //out.println(bookid);
    long time=System.currentTimeMillis();
    String favorite_id=time+" ";//该收藏记录的id
    java.text.SimpleDateFormat formatter=new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    java.util.Date currenttime=new java.util.Date();
    String date=formatter.format(currenttime);//收藏时间
    //out.println("usename"+username+"bookid"+bookid+"shuocangid"+favorite_id+"date"+date);
     %>
     
     <!-- 验证该用户是不是已收藏该书籍 -->
     <%
     
      String sql_query="select * from favorite where UserName='"+username+ "' and BookID='" + bookid+ "'";
      String sql_insert="insert into favorite values('"+favorite_id+ "','" +username+ "','" +bookid+ "','" +date+"')";
      ResultSet rs_query=DB.executeQuery(sql_query);
      if(rs_query.next())
      {
      out.println("<script language='javascript'>alert('您已收藏该书籍!');window.location.href='index.jsp';</script>"); 
      //response.sendRedirect("index.jsp");
      }
      else
      {
      DB.executeUpdate(sql_insert);
      out.println("<script language='javascript'>alert('收藏成功!');window.location.href='index.jsp';</script>"); 
      //response.sendRedirect("index.jsp");
      }
     
      %>
  </body>
</html>

javabean代码

package lytjb;
import java.sql.*;
//1个用于查找数据源的工具类。
public class DB {
private Connection con = null;
private Statement stmt = null;
ResultSet rs = null;


public ResultSet executeQuery(String sql) throws Exception {

try{  
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");  
con=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=SHB","sa","1234567");  
//Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");  
//con=DriverManager.getConnection("jdbc:odbc:JDBCSQLDemo_JSPTest");  
stmt=con.createStatement();  
rs=stmt.executeQuery(sql);
}catch(Exception e){}
return rs;
}


// 履行Insert,Update语句
public void executeUpdate(String sql) throws Exception {
try{  
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");  
con=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;DatabaseName=SHB","sa","1234567");  
//Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");  
//con=DriverManager.getConnection("jdbc:odbc:JDBCSQLDemo_JSPTest");  
stmt=con.createStatement();  
int rs=stmt.executeUpdate(sql);

}catch(Exception e){}
}


// 关闭stmt和关闭连接
public void close_all() {
try {
stmt.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}


}


------分隔线----------------------------
------分隔线----------------------------

最新技术推荐