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做注冊頁面 信息怎麼傳到資料庫里
1、先寫一個實體類,就叫做User吧;
2、再寫hibernate的hbm配置文件,寫UserDAO介面;
3、在寫UserDAOHibernateImpl實現,在寫spring配置文件定義好Hibernate的各種屬性;
4、再寫一個UserService介面,然後就是根據spring的IOC寫UserServiceImpl;
5、寫struts,struts的action可以這樣寫:
java">publicclassRegisterAction{
privateUseruser;
publicUsergetUser(){
returnuser;
}
publicvoidSetUser(Useruser){
this.user=user;
}
publicStringexecute(){
String[]confs=
{"applicationContext.xml"};
ApplicationContextac=
(confs);
UserServiceuserService=(UserService)
ac.getBean("userService");
try{
userService.register(user);
return"success";
}catch(Exceptione){
return"error";
}
}
}
Ⅲ 如何在jsp頁面上實現點擊注冊按鈕,彈出一個窗體來注冊(類似於百度貼吧的登錄和注冊),求詳細代碼和注釋
jsp中的注冊彈出新窗口是通過window.open一個新頁面來實現的。
頁面register.jsp代碼如下:
<%@ page contentType="text/html; charset=gb2312" language="java" import="cn.wy.Pet.User" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>會員注冊例子講解</title>
<style type="text/css">
<!--
.STYLE1 {
color: #FF0000;
font-weight: bold;
}
.STYLE2 {color: #FF0000}
.STYLE3 {
font-size: 18px;
font-weight: bold;
}
-->
</style>
</head>
<body style="font-size:12px">
<form id="form1" name="form1" method="post" action="<%=actionStr%>reg">
<p align="center"><br />
<span class="STYLE3">用戶注冊</span></p>
<table width="582" height="302" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#BCACD2">
<tr>
<td width="80" align="right">用戶名:</td>
<td width="496" align="left"><input name="userName" type="text" id="userName" size="16" maxlength="16" />
<span class="STYLE1">*</span> 3~16位字母或者數字(如:8hack)</td>
</tr>
<tr>
<td align="right">密碼:</td>
<td align="left"><input name="password1" type="text" id="password1" size="16" maxlength="16" />
<span class="STYLE1">* </span> 3~16位字母或者數字(如:abc123)</td>
</tr>
<tr>
<td align="right">確認密碼:</td>
<td align="left"><input name="password2" type="text" id="password2" size="16" maxlength="16" />
<span class="STYLE1">*</span> 必須和上面輸入的密碼相同</td>
</tr>
<tr>
<td align="right">電子郵件:</td>
<td align="left"><input name="email" type="text" id="email" maxlength="20" />
<span class="STYLE1">*</span> 找回密碼和聯系用(如:[email protected])</td>
</tr>
<tr>
<td align="right">聯系電話:</td>
<td align="left"><input name="tel" type="text" id="tel" size="20" maxlength="20" />
如(0871-8888888,13888853113)</td>
</tr>
<tr>
<td align="right">聯系地址:</td>
<td align="left"><input name="address" type="text" id="address" maxlength="50" /></td>
</tr>
<td height="40" colspan="2" align="center"><input type="submit" name="Submit" value="確認注冊" />
<input type="reset" name="Submit2" value="重新填寫" /></td>
</tr>
</table>
</form>
</body>
</html>
後台servlet的處理:
public class reg extends HttpServlet
{
public reg()
{
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
PrintWriter out;
DBConnection dbc=null;
String userName;
String psd;
String email;
String tel;
String address;
int popedom;
response.setContentType("text/html;charset=UTF-8");
out = response.getWriter();
try{
dbc = new DBConnection();
PreparedStatement ps = null;
userName = request.getParameter("userName");
psd = login.encrypt(request.getParameter("password1").toString());
email = request.getParameter("email");
tel = request.getParameter("tel");
address = request.getParameter("address");
popedom = Integer.parseInt(request.getParameter("popedom"));
if (userName != null && psd != null && email != null)
{
ps = dbc.getCon().prepareStatement("insert into [User](UName,Upass,UEmail,UTel,UAddress,UPopedom) values(?,?,?,?,?,?)");
ps.setString(1, userName);
ps.setString(2, psd);
ps.setString(3, email);
ps.setString(4, tel);
ps.setString(5, address);
ps.setInt(6, popedom);
ps.execute();
System.out.print("新用戶注冊:" + request.getParameter("userName") + " ");
out.print("<script>alert('恭喜您:注冊成功!現已經登錄到網站!');history.go(-1)</script>");
}
if (dbc != null)
dbc.dbClose();
}
catch(SQLException ex)
{
out.print("<script>alert('注冊失敗!資料庫連接錯誤!');history.go(-1)</script>");
ex.printStackTrace();
if (dbc != null)
dbc.dbClose();
}
}
}
Ⅳ jsp編程用戶注冊
樓上的那位寫的真是夠簡單的!
Ⅳ jsp登錄注冊
regeditIn.jsp 在這個文件裡面取得表單提交的數據,驗證一下是否正確,然後存入資料庫。
以下是我寫的一個頁面,不過用的是Oracle函數。
<%@page contentType="text/html; charset=GBK"%>
<%@page import="java.sql.*"%>
<%@page import="javax.sql.*"%>
<%request.setCharacterEncoding("GBK");
response.setContentType("text/html;charset=GBK");%>
<%!
private String submit(String name,String sex,String phone,String sheng,String shi,String xian,String email,String size,String price){
String ret="error!!";
Connection conn=null;
CallableStatement cstmt=null;
try{
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@192.168.1.101:1521:daredo";
String user="1211";
String password="233243434343";
conn=DriverManager.getConnection(url,user,password);
cstmt=conn.prepareCall("{call ?:=fun_haier_tuangou(?,?,?,?,?,?,?,?,?)}");
cstmt.registerOutParameter(1, Types.INTEGER);
cstmt.setString(2,name);
cstmt.setString(3,sex);
cstmt.setString(4,phone);
cstmt.setString(5,sheng);
cstmt.setString(6,shi);
cstmt.setString(7,xian);
cstmt.setString(8,email);
cstmt.setString(9,size);
cstmt.setString(10,price);
cstmt.execute();
ret = cstmt.getString(1);
cstmt.close();
cstmt = null;
conn.close();
conn = null;
}
catch (Exception e) {
ret=e.toString();
e.getStackTrace();
}
finally {
if (cstmt != null) {
try {
cstmt.close();
}
catch (SQLException ex1) {
ex1.getCause();
}
cstmt = null;
}
if (conn != null) {
try {
conn.close();
}
catch (SQLException ex1) {
}
conn = null;
}
}
return ret;
}
%>
<%
String name=new String(request.getParameter("name").trim().getBytes("8859_1"));
String sex=new String(request.getParameter("sex").trim().getBytes("8859_1"));
String phone=new String(request.getParameter("phone").trim().getBytes("8859_1"));
String sheng=new String(request.getParameter("sheng").trim().getBytes("8859_1"));
String shi=new String(request.getParameter("shi").trim().getBytes("8859_1"));
String xian=new String(request.getParameter("xian").trim().getBytes("8859_1"));
String email=new String(request.getParameter("email").trim().getBytes("8859_1"));
String size=new String(request.getParameter("size").trim().getBytes("8859_1"));
String price=new String(request.getParameter("price").trim().getBytes("8859_1"));
out.println("開始!!"+name+","+sex+","+phone+","+sheng+","+shi+","+xian+","+email+","+size+","+price);
String ret=this.submit(name,sex,phone,sheng,shi,xian,email,size,price);
out.println(ret);
%>
<script language=javascript>alert("提交成功!");window.location.href='tuangou.jsp';</script>
Ⅵ 用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>
<tr><td> 輸入用戶名:</td>
<td><input type=text name=userid ></td>
</tr>
<tr><td>輸入密碼:</td>
<td><input type=password name=password></td>
</tr>
<tr><td><input 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="adser.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>
Ⅷ 怎樣jsp實現用戶注冊登錄查詢
純屬學生作品,隨便一本書上就能找到源代碼
Ⅸ jsp 登陸和注冊問題
一般地,沒必要就不要用JSP代碼:
<html>
<head>
<title>用戶登陸</title>
<script language="JavaScript" type="text/JavaScript">
//return false不會提交,true則提交
function check() {
var userName = document.all("userName").value;
var password = document.all("password").value;
if (userName == "") {
alert('用戶名不能為空');
return false;
} else if (password == "") {
alert('密碼不能為空');
return false;
}
return true;
}
</script>
</head>
<body onload="focus()">
<Form id="login" name="login" action="請求路徑"
onsubmit="return check()">
<input name='userName' id='userName'>
<input name='password' id='password'>
<input type="submit" value='submit'>
</Form>
</body>
</html>
Ⅹ jsp登陸注冊功能實現原理
原理是jsp注冊功能原理。注冊功能則是將動態的sql會話中的sql語句改變即可,原理相同。為了快速的驗證登錄,login頁面就只傳一個sname。