本文目錄一覽:
- 1、在JSP中註冊頁面,點擊提交按鈕以後怎麼樣讓信息入錄到資料庫?
- 2、JSP 註冊之後往 資料庫中添加數據 添加之後資料庫有記錄顯示 但是顯示值為空 代碼如下
- 3、jsp中使用表單將用戶註冊信息提交到SQL資料庫
- 4、怎麼用jsp做註冊頁面 信息怎麼傳到資料庫里
- 5、jsp做登錄,註冊頁面 資料庫
在JSP中註冊頁面,點擊提交按鈕以後怎麼樣讓信息入錄到資料庫?
當點擊提交按鈕時所有的表單元素都作為參數傳到後台等待處理,在後台的servlet里我們可以能過方法 request.getParameter(name)得到表單中的內容,其中name是表單元素的NAME屬性名。得到表單元素內容後就可以大膽的用JDBC語句向資料庫中插入數據了。
JSP 註冊之後往 資料庫中添加數據 添加之後資料庫有記錄顯示 但是顯示值為空 代碼如下
很明顯,原因是你的username password 沒有取到值,也就是FORM提交的問題
String username=request.getParameter(“username”);
這一句中你沒有得到username的值
jsp中使用表單將用戶註冊信息提交到SQL資料庫
用JDBC連接資料庫啊 很簡單的事情 給你個例子連接資料庫的
Import java.sql.*;
Import javax.naming.*;
Import javax.sql.DataSource;
public class DbConn {
private static DataSource ds = null;
private static Connection conn = null;
// 使用JDBC連接資料庫
public static Connection getConn_jdbc() {
try {
String url = “jdbc:mysql://localhost:3306/bbsdb”;
String username = “root”;
String password = “pla”;
Class.forName(“com.mysql.jdbc.Driver”).newInstance();
conn = DriverManager.getConnection(url, username, password);
return conn;
} catch (Exception e) {
System.err.println(“資料庫連接異常: ” + e.getMessage()); return null;
} }
// 關閉資料庫連接
剩下該你了 創建實體類 然後把數據插入到資料庫
怎麼用jsp做註冊頁面 信息怎麼傳到資料庫里
1、先寫一個實體類,就叫做User吧;
2、再寫hibernate的hbm配置文件,寫UserDAO介面;
3、在寫UserDAOHibernateImpl實現UserDAO,在寫spring配置文件定義好Hibernate的各種屬性;
4、再寫一個UserService介面,然後就是根據spring的IOC寫UserServiceImpl;
5、寫struts,struts的action可以這樣寫:
public class RegisterAction{
private User user;
public User getUser(){
return user;
}
public void SetUser(User user){
this.user=user;
}
public String execute(){
String[] confs =
{“applicationContext.xml”};
ApplicationContext ac =
new ClassPathXmlApplicationContext(confs);
UserService userService = (UserService)
ac.getBean(“userService”);
try{
userService.register(user);
return “success”;
}catch(Exception e){
return “error”;
}
}
}
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-tw/n/289503.html