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。