How JSP uses JavaBean

Source: Internet
Author: User

JavaBean generally placed in the SRC directory

JSP files are generally placed under Webroot

SRC and Webroot in the same directory

Here is my directory structure, with JSP code and JavaBean code


JSP code

<%@ 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" >
<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" >
-



<body bgcolor= "C0FFC0" >
<jsp:usebean id= "DB" scope= "page" class= "LYTJB. DB "></jsp:useBean>
<%
String Username= (String) session.getattribute ("username_session");//Get Current User
String bookid=request.getparameter ("BookID");//Get the ID of a collection book
Out.println (BookID);
Long Time=system.currenttimemillis ();
String favorite_id=time+ "";//ID of the collection record
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);//Collection Time
Out.println ("Usename" +username+ "BookID" +bookid+ "Shuocangid" +favorite_id+ "date" +date);
%>

<!--Verify that the user has already collected the book--
<%

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 (' You've collected the book! '); window.location.href= ' index.jsp ';</script> ");
Response.sendredirect ("index.jsp");
}
Else
{
Db.executeupdate (Sql_insert);
Out.println ("<script language= ' JavaScript ' >alert (' collection succeeded! '); window.location.href= ' index.jsp ';</script> ");
Response.sendredirect ("index.jsp");
}

%>
</body>

JavaBean Code

Package lytjb;
Import java.sql.*;
A tool class that is used to find the data source.
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;
}


Execute insert,update statement
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) {}
}


Close the stmt and close the connection
public void Close_all () {
try {
Stmt.close ();
Con.close ();
} catch (SQLException e) {
E.printstacktrace ();
}
}


}


How JSP uses JavaBean

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.