Jsp link database, jsp Link

Source: Internet
Author: User

Jsp link database, jsp Link

Database Table Code:

/*
Navicat MySQL Data Transfer

Source Server: localhost_3306
Source Server Version: 50528
Source Host: localhost: 3306
Source Database: bbs

Target Server Type: MYSQL
Target Server versions: 50528
File Encoding: 65001

Date: 09:25:01
*/

SET FOREIGN_KEY_CHECKS = 0;

------------------------------
-- Table structure for tb_user
------------------------------
Drop table if exists 'tb _ user ';
Create table 'tb _ user '(
'Id' int (11) not null AUTO_INCREMENT,
'Username' varchar (255) not null,
'Password' varchar (255) not null,
'Linkman' varchar (255) default null,
'Company' varchar (255) default null,
'Companytel' varchar (255) default null,
'Qq' varchar (255) default null,
Primary key ('id ')
) ENGINE = InnoDB AUTO_INCREMENT = 3 default charset = utf8;

------------------------------
-- Records of tb_user
------------------------------
Insert into 'tb _ user' VALUES ('1', 'admin', 'admin', null );
Insert into 'tb _ user' VALUES ('2', 'user', 'user', null );

 

Registration page:

<% @ Page language = "java" import = "java. util. *, java. SQL. *" pageEncoding = "UTF-8" %>
<%
String path = request. getContextPath ();
String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/";
%>
<% @ Page import = "java. SQL. *" %>
<%!
Public static final String DBDRIVER = "com. mysql. jdbc. Driver ";
Public static final String userName = "root"; // userName for Logon
Public static final String userPasswd = "taizhen"; // logon mysql password
Public static final String dbName = "test"; // Database Name
Public static final String tableName = "user"; // table name
Public static final String DBURL = "jdbc: mysql: // localhost: 3306/" + dbName + "? User = "+ userName +" & password = "+ userPasswd;
%>

<%
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
Boolean flag = false; // indicates the mark of successful or failed login.
%>

<%

String action = request. getParameter ("action ");
If (null = action ){

} Else {
%>

<% Out. println (action); %>
<%
String name = request. getParameter ("username"); // receives the form parameter
String password = request. getParameter ("password"); // receives the form parameter
Try {
Class. forName (DBDRIVER );
Conn = DriverManager. getConnection (DBURL );
String SQL = "SELECT name, password FROM user WHERE name =? AND password =? ";
Pstmt = conn. prepareStatement (SQL );
Pstmt. setString (1, name );
Pstmt. setString (2, password );
Rs = pstmt.exe cuteQuery ();
While (rs. next ()){
// If there is content, execute here, which indicates that the query is made and the query is valid.
Flag = true;
}
} Catch (Exception e ){
} Finally {
Try {
Conn. close (); // connection 1 is closed, and all operations are closed.
} Catch (Exception e ){}
}
%>
<% Out. println (flag); %>
<% Out. println (action); %>
<%

If (flag ){
Request. getSession (). setAttribute ("success", "login ");
%>
<Jsp: forward page = "success. jsp"/>
<%
} Else {// Login Failed, jump to failure. jsp
Request. getSession (). setAttribute ("error", "Logon ");

%>
<Jsp: forward page = "error. jsp"/>
<%
}
}

%>
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>
<Base href = "<% = basePath %>">

<Title> My JSP 'login. jsp 'starting page </title>

</Head>

<Body>
<Form action = "login. jsp" method = "post">
<Input type = "hidden" name = "action"/>
User name:
<Input type = "text" name = "username" value = "username"/>
<Br>
Password:
<Input type = "password" name = "password" value = "password"/>
<Br>
<Input type = "submit" value = "login"/>
</Form>

</Body>
</Html>

 

Jsp link database code:

<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %>
<%
String path = request. getContextPath ();
String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/";
%>
<% @ Page import = "java. SQL. *" %>
<%!
Public static final String DBDRIVER = "com. mysql. jdbc. Driver ";
Public static final String userName = "root"; // userName for Logon
Public static final String userPasswd = "taizhen"; // logon mysql password
Public static final String dbName = "test"; // Database Name
Public static final String tableName = "user"; // table name
Public static final String DBURL = "jdbc: mysql: // localhost: 3306/" + dbName + "? User = "+ userName +" & password = "+ userPasswd;
%>

<%
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
Boolean flag = false; // indicates the mark of successful or failed login.
%>

<%

String action = request. getParameter ("username ");
If (action! = Null ){

%>

<% Out. println (request. getParameter ("password"); %>
<%
String name = request. getParameter ("username"); // receives the form parameter
String password = request. getParameter ("password"); // receives the form parameter
String linkman = request. getParameter ("linkman ");
String qq = request. getParameter ("qq ");

Try {
Class. forName (DBDRIVER );
Conn = DriverManager. getConnection (DBURL );
String SQL = "INSERT INTO user (name, password) VALUES (?,?) ";

Pstmt = conn. prepareStatement (SQL );
Pstmt. setString (1, name );
Pstmt. setString (2, password );

Int SQL _update = pstmt.exe cuteUpdate ();
If (SQL _update> 0 ){
Flag = true;
}
} Catch (Exception e ){
} Finally {
Try {
Conn. close (); // connection 1 is closed, and all operations are closed.
} Catch (Exception e ){}
}
%>


<%

If (flag ){
Request. getSession (). setAttribute ("success", "register ");
%>
<Jsp: forward page = "success. jsp"/>
<%
} Else {// Login Failed, jump to failure. jsp
Request. getSession (). setAttribute ("error", "register ");

%>
<Jsp: forward page = "error. jsp"/>
<%
}


} Else {}
%>

<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>


<Title> My JSP 'register. jsp 'starting page </title>

</Head>

<Body>

<Div class = "loginbox registbox">

<Div class = "login-content reg-content">
<Div class = "loginbox-title">
<H3> Registration </Div>
<Form id = "signupForm" action = "register. jsp" method = "post">

<Div class = "row">
<Label class = "field" for = "email"> User Name </label>
<Input type = "text" class = "input-text-user noPic input-click" name = "username" id = "email">
</Div>
<Div class = "row">
<Label class = "field" for = "password"> password </label>
<Input type = "password" value = "" class = "input-text-password noPic input-click" name = "password" id = "password">
</Div>

<Div class = "row">
<Label class = "field" for = "contact"> contact </label>
<Input type = "text" value = "" class = "input-text-user noPic input-click" name = "linkman" id = "contact">
</Div>
<Div class = "row">
<Label class = "field" for = "company"> company name </label>
<Input type = "text" value = "" class = "input-text-user noPic input-click" name = "company" id = "company">
</Div>
<Div class = "row">
<Label class = "field" for = "tel"> company phone number </label>
<Input type = "text" value = "" class = "input-text-user noPic input-click" name = "companytel" id = "tel">
</Div>
<Div class = "row">
<Label class = "field" for = "qq"> QQ </label>
<Input type = "text" value = "" class = "input-text-user noPic input-click" name = "qq" id = "qq">
</Div>

<Div class = "row btnArea">
<Button class = "login-btn" id = "submit"> register </button>
</Div>
</Form>
</Div>
<Div class = "go-regist">
You already have an account. <a href = "/Demo/login. jsp" class = "link"> log on </a>
</Div>
</Div>
</Body>
</Html>

 

Logon successful:

<% @ 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 'success. jsp 'starting page </title>


</Head>

<Body>
$ {SessionScope. success} successful! <Br>
</Body>
</Html>

 

Logon Failed:

<% @ 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 'error. jsp 'starting page </title>

</Head>

<Body>
$ {SessionScope. error} failed <br>
</Body>
</Html>

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.