jsp登錄註冊代碼數據庫,jsp實現數據庫註冊

本文目錄一覽:

jsp連接mysql數據庫註冊用戶代碼的問題

stat=conn.prepareStatement(“insert wei values(?,?,?)”);

stat.setString(1,reg_name);

stat.setString(2,reg_pass);

stat.setString(3,reg_pass1);

if(reg_pass1!=(reg_pass)){

out.println(“scriptalert(‘密碼不一致,請重新輸入!’);location.replace(‘zhuce.jsp’)/script”);

flag=false;

}

stat.executeUpdate();

這塊代碼把 if(reg_pass1!=(reg_pass))改成if(!reg_pass1.equals(reg_pass)),要解決為什麼插入數據庫的問題,在stat.executeUpdate();加個判斷if(!flag){

stat.executeUpdate();

}

求大神寫一下jsp的簡單的註冊界面代碼。

1.需要一個jsp頁面:

//login.jsp核心代碼:

form action=”${pageContext.request.contextPath}/servlet/UserServlet” method=”post”

input type=”text” name=”loginname” /input type=”password” name=”password”/

input type=”submit” value=”登錄”/

/form

2.需要一個servlet來驗證登錄信息

//UserServlet 核心代碼

class UserServlet extends HttpServlet{

protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {

process(request, response);

}

protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {

process(request, response);

}

private void process(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {

PrintWriter pw = response.getWriter();

request.setCharacterEncoding(“UTF-8”);

response.setContentType(“text/html”);

String loginname = request.getParameter(“loginname”);

String password = request.getParameter(“password”);

//創建一個service來處理業務邏輯(包括查詢數據庫操作)

UserService service = new UserService();

boolean bool = service.validateUser(loginname,password);

if(!bool){

pw.println(“用戶名或密碼錯誤”);

}else{

pw.println(“登錄成功”);

}

}

3.需要一個service處理業務邏輯(包括查詢數據庫操作)

//UserService 核心代碼

public class UserService{

/**

*查詢數據庫驗證用戶是否存在,返回boolean

*/

public boolean validateUser(String loginname,String password){

boolean bool = false;

Connection conn = null;

PreparedStatement ps = null;

//這裡以mysql為例

try {

Class.forName(“com.mysql.jdbc.Driver”).newInstance();

conn = DriverManager.getConnection(“jdbc:mysql://localhost:3306/test”, “root”, “”);

String sql = “select login_name,pass_word from t_user where login_name=? and pass_word=?”;

ps = conn.prepareStatement(sql);

ps.setString(0, loginname);

ps.setString(1, password);

ResultSet rs = ps.executeQuery();

if(rs.next()){

bool = true;

}

} catch (Exception e) {

e.printStackTrace();

} finally{

try {

if(conn != null){

conn.close();

conn = null;

}

if(ps != null){

ps.close();

ps = null;

}

} catch (SQLException e) {

e.printStackTrace();

}

}

return bool;

}

}

這是一段JSP實現登錄註冊並鏈接數據庫頁面的代碼,改這段代碼的哪一部分才能連接到我指定的賬號數據庫

主要哈哈這三行

String url = “jdbc:sqlserver://localhost:1433; DatabaseName = 你的數據庫名”;

String username = “sa”; 你的連接用戶

String password = “123”; 你的密碼

用jsp連接數據庫實現登錄註冊

建議使用html+servlet或者jsp+servlet 通過ajax將數據提交到後台servlet校驗 可實現無刷新提交。

jsp做登錄,註冊頁面 數據庫

jsp登錄註冊頁面都需要查詢和插入數據庫的,還要檢查註冊信息存不存在。

完整例子如下:

用戶信息的bean:

package chen;

public class UserBean

{

private String userid;

private String password;

public void setUserId(String userid)

{

this.userid=userid;

}

public void setPassword(String password)

{

this.password=password;

}

public String getUserId()

{

return this.userid;

}

public String getPassword()

{

return this.password;

}

}

提交數據庫的bean:

package chen;

import com.mysql.jdbc.Driver;

import java.sql.*;

public class UserRegister

{

private UserBean userBean;

private Connection con;

//獲得數據庫連接。

public UserRegister()

{

String url=”jdbc:mysql://localhost/”+”chao”+”?user=”+”root”+”password=”+”850629″;

try

{

Class.forName(“com.mysql.jdbc.Driver”).newInstance();

con = DriverManager.getConnection(url);

}

catch(Exception e)

{

e.printStackTrace();

}

}

//設置待註冊的用戶信息。

public void setUserBean(UserBean userBean)

{

this.userBean=userBean;

}

//進行註冊

public void regist() throws Exception

{

String reg=”insert into userinfo(userid,password) values(?,?)”;

try

{

PreparedStatement pstmt=con.prepareStatement(reg);

pstmt.setString(1,userBean.getUserId());

pstmt.setString(2,userBean.getPassword());

pstmt.executeUpdate();

}

catch(Exception e)

{

e.printStackTrace();

throw e;

}

}

}

提交註冊數據進入數據庫:

%@ page contentType=”text/html;charset=gb2312″ pageEncoding=”gb2312″

import=”chen.*” %

jsp:useBean id=”userBean” class=”chen.UserBean” scope=”request”

jsp:setProperty name=”userBean” property=”*”/

/jsp:useBean

jsp:useBean id=”regist” class=”chen.UserRegister” scope=”request”/

html

head

title 用戶信息註冊頁面/title

meta http-equiv=”Content-Type” content=”text/html; charset=gb2312″

/head

body

%

String userid =request.getParameter(“userid”);

String password = request.getParameter(“password”);

userBean.setUserId(userid);

userBean.setPassword(password);

System.out.println(userid+password);

%

% try{

regist.setUserBean(userBean);

out.println(userid);

regist.regist();

out.println(“註冊成功”);}

catch(Exception e){

out.println(e.getMessage());

}

%

br

a href=”login.jsp”返回/a

/body

/html

登陸驗證頁面:

%@page import=”java.sql.*” contentType=”text/html;charset=GB2312″ %

%@page import=”java.util.*”%

%

String userid1=new String(request.getParameter(“userid”));

String password1=new String(request.getParameter(“password”));

Class.forName(“com.mysql.jdbc.Driver”);

Connection con=DriverManager.getConnection(“jdbc:mysql://localhost:3306/chao”,”root”,”850629″);

Statement stmt=con.createStatement();

String sql=”select * from userinfo where userid='”+userid1+”‘;”;

ResultSet rs=stmt.executeQuery(sql);

if(rs.next())

{String password=new String(rs.getString(“password”));

if(password.equals(password1))

{session.setAttribute(“userid1”,userid1);

response.sendRedirect(“sucess.jsp”);

}

else

{response.sendRedirect(“login.jsp”);

}

}

else

{response.sendRedirect(“login.jsp”);

}

%

登陸頁面:

%@ page contentType=”text/html; charset=gb2312″ %

html

body

form method=”get” action=”checklogin.jsp”

table

trtd 輸入用戶名:/td

tdinput type=text name=userid /td

/tr

trtd輸入密碼:/td

tdinput type=password name=password/td

/tr

trtdinput type=submit value=確認

/td/tr

/table

/form

form action=”register.jsp”

input type=submit value=註冊

/form

/body

/html

註冊頁面:

%@page contentType=”text/html; charset=gb2312″ language=”java” import=”java.util.*,java.io.*”%

html

head

meta http-equiv=”Content-Type” content=”text/html; charset=gb2312″

/head

body

center

h1註冊新用戶/h1

form action=”adduser.jsp” method=post

table border=”1″ bgcolor=”#0099CC”

tr

td 用戶名:

input type=”text” name=”userid”

/td

/tr

tr valign=”middle”

td 密碼:

input type=”password” name=”password” readonly

/td

/tr

tr

td

input type=submit value=提交

/td

/tr

/table

/form

/center

/body

/html

登陸成功頁面:

%@page import=”java.util.*” contentType=”text/html; charset=gb2312″ %

%@include file=”trans.jsp”%

html

head

title

sucess

/title

/head

body bgcolor=”#ffffff”

h1

登錄成功,歡迎您!

/h1%=trans(session.getAttribute(“userid1”))%

/body

/html

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/159172.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-11-19 19:00
下一篇 2024-11-19 19:00

相關推薦

  • Python周杰倫代碼用法介紹

    本文將從多個方面對Python周杰倫代碼進行詳細的闡述。 一、代碼介紹 from urllib.request import urlopen from bs4 import Bea…

    編程 2025-04-29
  • Python字符串寬度不限制怎麼打代碼

    本文將為大家詳細介紹Python字符串寬度不限制時如何打代碼的幾個方面。 一、保持代碼風格的統一 在Python字符串寬度不限制的情況下,我們可以寫出很長很長的一行代碼。但是,為了…

    編程 2025-04-29
  • Python基礎代碼用法介紹

    本文將從多個方面對Python基礎代碼進行解析和詳細闡述,力求讓讀者深刻理解Python基礎代碼。通過本文的學習,相信大家對Python的學習和應用會更加輕鬆和高效。 一、變量和數…

    編程 2025-04-29
  • Python 常用數據庫有哪些?

    在Python編程中,數據庫是不可或缺的一部分。隨着互聯網應用的不斷擴大,處理海量數據已成為一種趨勢。Python有許多成熟的數據庫管理系統,接下來我們將從多個方面介紹Python…

    編程 2025-04-29
  • Python滿天星代碼:讓編程變得更加簡單

    本文將從多個方面詳細闡述Python滿天星代碼,為大家介紹它的優點以及如何在編程中使用。無論是剛剛接觸編程還是資深程序員,都能從中獲得一定的收穫。 一、簡介 Python滿天星代碼…

    編程 2025-04-29
  • 倉庫管理系統代碼設計Python

    這篇文章將詳細探討如何設計一個基於Python的倉庫管理系統。 一、基本需求 在着手設計之前,我們首先需要確定倉庫管理系統的基本需求。 我們可以將需求分為以下幾個方面: 1、庫存管…

    編程 2025-04-29
  • openeuler安裝數據庫方案

    本文將介紹在openeuler操作系統中安裝數據庫的方案,並提供代碼示例。 一、安裝MariaDB 下面介紹如何在openeuler中安裝MariaDB。 1、更新軟件源 sudo…

    編程 2025-04-29
  • 寫代碼新手教程

    本文將從語言選擇、學習方法、編碼規範以及常見問題解答等多個方面,為編程新手提供實用、簡明的教程。 一、語言選擇 作為編程新手,選擇一門編程語言是很關鍵的一步。以下是幾個有代表性的編…

    編程 2025-04-29
  • Python實現簡易心形代碼

    在這個文章中,我們將會介紹如何用Python語言編寫一個非常簡單的代碼來生成一個心形圖案。我們將會從安裝Python開始介紹,逐步深入了解如何實現這一任務。 一、安裝Python …

    編程 2025-04-29
  • 怎麼寫不影響Python運行的長段代碼

    在Python編程的過程中,我們不可避免地需要編寫一些長段代碼,包括函數、類、複雜的控制語句等等。在編寫這些代碼時,我們需要考慮代碼可讀性、易用性以及對Python運行性能的影響。…

    編程 2025-04-29

發表回復

登錄後才能評論