jsp如何使用javabean,jsp使用javabean

來源:互聯網
上載者:User

jsp如何使用javabean,jsp使用javabean

javabean一般放在src目錄下

jsp檔案一般放在webroot下

src和webroot在同一個目錄下

下面是我的目錄結構,附帶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.*;
//一個用於尋找資料來源的工具類。
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();
}
}


}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.