我写的一个登陆页面 可以选择登陆和注册 如果我事先在数据库(sqlsever 2000)写好id和password 就可以登陆成功 但是要是通过注册页面注册的id和password就会提示密码错误 而且写到数据库中的其他项目 如 年龄 性别 有乱码 请教大家 是不是设置的数据类型不对 ?
解决方案 »
- 请问下高手页面上文章分页的问题
- 求助!Eclipse3.5(Galileo)安装HibernateTools遇到问题!!!
- TOMCAT启动显示java.security.policy:添加项目错误
- 当有多个按钮提交一个表单是,在servlet怎么区分是哪个按钮提交的?
- 用jk2整合IIS和TOMCAT只能看到JSP页,但看不到servlet的。请高手指点一下
- 熟悉javaBean的大侠救一下!
- 急问:在Tomcat 5配置数据源的时候遇到的问题
- 菜鸟问题!在线等,急!
- 高手、低手、黑手统统进来!
- Servlet and Tomcat ???
- 急~tomcat在浏览器的路径问题(看了很多答案还是解决不了...)
- 向MySQL中插入一篇文章,当在网页上显示时,要保持原来的段落换行什么的,怎么弄啊????
这个项目里有比较完整的过滤器编写、应用、配置
login.jsp:
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="error.jsp"%><!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link rel="stylesheet" href="register.css" type="text/css">
<title>login</title><script language="javascript">
<!--
function loginsubmit()
{
ID=document.form.ID.value;
password=document.form.password.value;
if(ID==""||ID.length>10)
{
alert("ID is empty or too long!!!");
document.form.ID.focus();
}
else if(password==""||password.length<3)
{
alert("password is empty or too short!!!");
document.form.ID.focus();
}
else{
document.form.submit();
}
}
-->
</script>
</head>
<body>
<table width="90%" border="0" cellspacing="0" cellpadding="0" align="center">
<tr><td height="35"> </td></tr>
<tr><td height="1" align="center">
<%
String info=request.getParameter("info");
if(("1").equals(info))
out.println("<font size=4 color='red'>NO ID</font><br><br><br>");
else if(("2").equals(info))
out.println("<font size=4 color='red'>PSW WRONG</font><br><br><br>");else
out.println("<font size=4 color='red'>login</font><br><br><br>");
%>
</td></tr>
<tr>
<td align="center">
<!--登陆处理-->
<form method=post action="check_login.jsp" name="form">
<table width="170" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><img src="images/join.gif" border="0"></td>
</tr>
</table>
<table width="170" bgcolor="d2e8ff" style="border-bottom:1px solid #0079ce; border-left:1px solid #0079ce; border-right:1px solid #0079ce">
<tr><td height="5"></td></tr>
<tr><td>ID:</td>
<td><input type="text" name="ID" ></td></tr>
<tr><td width="56">Password:</td>
<td width="113"><input type="password" name="password"></td></tr>
<tr>
<td align="center" colspan="2" height="30">
<a href="javascript:loginsubmit();">login</a>
<a href="register.jsp">regist</a> </td>
</tr>
<tr><td colspan="2" align="right"></td></tr>
</table>
</form>
<!--登陆处理-->
</td>
</tr>
</table>
</body>
</html>check_login.jsp:处理登陆
<%@page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="error.jsp"%>
<%
String ID=request.getParameter("ID");
String password=request.getParameter("password");
String info="0";
Connection con=null;
Statement stm=null;
ResultSet rs=null;
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver").newInstance();//加入驱动
String url="jdbc:odbc:458";
con=DriverManager.getConnection(url,"sa","");
stm=con.createStatement();
rs=stm.executeQuery("select * from userinfo where ID='"+ID+"'");
if(rs.next())
{
if(rs.getString("password").equals(password))
{
response.sendRedirect("index.jsp");
session.setAttribute("user",ID);
}
else
response.sendRedirect("login.jsp?info=2");
}
else
response.sendRedirect("login.jsp?info=1");
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
if(rs!=null){
try{rs.close();}catch(Exception e){e.printStackTrace();}
}
if(stm!=null){
try{stm.close();}catch(Exception e){e.printStackTrace();}
}
if(con!=null){
try{con.close();}catch(Exception e){e.printStackTrace();}
}
}
%>
register.jsp:注册页面
<%@ page contentType="text/html;chraset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<html>
<head><title>校友录注册</title></head>
<meta http-equiv="content-type" content="text/html; charset=utf8">
<link rel="stylesheet" href="register.css"type+"text/css"/>
<%! int i;%>
<script language="javascript">
<!--
function checkform(){
if(document.form.ID.value==""||!(document.form.ID.value).indexOf("/")||!(document.form.ID.value).indexOf("/")){
alert("用户名为空或者有非法字符!!!");
document.form.ID.focus();
}
else if(document.form.password.value==""||!(document.form.password.value).length<=3){
alert("密码不能为空且不能小于三位");
document.form.password.focus();
}
else if((document.form.rpassword.value)!=(document.form.password.value)){
alert("两次密码输入不一致");
document.form.rpassword.focus();
}
else if(document.form.name.value==""){
alert("姓名不能为空");
document.form.name.focus();
}
else{
document.form.submit();
}
}
//-->
</script>
<body>
<table width="80%" border="0" cellspacing="0"cellpadding="0" align="center">
<tr><td height="5"> </td></tr>
<tr>
<td>
<div align="center"><font size=3><strong>用户注册</strong></font><br>
</td>
</tr>
<tr>
<td>
<form method=post action="do_register.jsp" name="form">
<table width="80%" border="0" cellspacing="0"cellpadding="0" align="center">
<tr height="30">
<td width="20%" align="right"> ID:</td>
<td width="40%" align="left"> <input type="text" name="ID"> </td>
<td width="40%" align="left"> <font color="0099ff">用户名请使用数字或者字母</font></td>
</tr>
<tr height="30">
<td width="20%" align="right"> password:</td>
<td width="40%" align="left"> <input type="password" name="password"> </td>
<td width="40%" align="left"> <font color="0099ff">密码要使用三位以上</font></td>
</tr>
<tr height="30">
<td width="20%" align="right"> repassword:</td>
<td width="40%" align="left"> <input type="password" name="password"> </td>
<td width="40%" align="left"> </td>
</tr>
<tr height="30">
<td width="20%" align="right"> name:</td>
<td width="40%" align="left"> <input type="text" name="name" size="15"> </td>
<td width="40%" align="left"> </td>
</tr>
<tr height="30">
<td width="20%" align="right"> sex:</td>
<td width="40%" align="left">
<input type="radio" name="sex" value="男" checked>男
<input type="radio" name="sex" value="女">女
</td>
<td width="40%" align="left"> </td>
</tr>
<tr height="30">
<td width="20%" align="right"> birthday:</td>
<td width="40%" align="left">
<select name="year"><% for(int i=1970;i<=2009;i++) { %>
<option value=<%=i%>><%=i%></option><%}%></select>year
<select name="month"><% for(i=1;i<=12;i++) { %>
<option value=<%=i%>><%=i%></option><%}%></select>month
<input type="text" name="day" size="5">day
</td>
<td width="40%" align="left"> </td>
</tr>
<tr height="30">
<td width="20%" align="right"> intruoduce:</td>
<td width="40%" align="left">
<textarea name="introduce" cols="30" rows="5"></textarea></td>
</tr>
<tr height="30">
<td width="20%" align="right"> </td>
<td width="40%" align="left"> </td>
<td width="40%" align="left"> </td>
</tr>
<tr>
<td colspan=3 align="center"><hr width="80%" size="1"></td>
</tr>
<tr height="30">
<td colspan=3 align="center">
<a href="javascript:checkform();"></a>
<a href="javascript:document.form.reset()">reset</a>
<input type="submit" name="reg" value="注册">
</td>
</tr>
</table>
</form>
</td>
</tr>
</table>
</body>
</html>
do_register.jsp:
<%@ page contentType="text/html; charset=gb2312" language="java" %><%@ page import="java.sql.*"%>
<%@ page import="java.text.ParsePosition"%>
<%@ page import="java.text.SimpleDateFormat"%>
<%@ page import="java.util.Date"%>
<%!
long getDate(String s)
{
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-mm-dd");
ParsePosition parsePosition=new ParsePosition(0);
Date date=simpleDateFormat.parse(s,parsePosition);
return date.getTime();
}
%>
<%
String ID=request.getParameter("ID");
ID=ID.trim();
String password=request.getParameter("password");
password=password.trim();
String rpassword=request.getParameter("rpassword");String name=request.getParameter("name");String sex=request.getParameter("sex");
String year=request.getParameter("year");
//if(year.length==1) year="0"+year;
String month=request.getParameter("month");
//if(month.length==1) month="0"+month;
String day=request.getParameter("day");
//if(day.length==1) day="0"+day;
String date=year+"-"+month+"-"+day;
String introduce=request.getParameter("introduce");
Connection con=null;
PreparedStatement prepare=null;try
{
String DRIVER="sun.jdbc.odbc.JdbcOdbcDriver";
String URL="jdbc:odbc:458";
Class.forName(DRIVER); con= DriverManager.getConnection(URL,"sa","");
prepare=con.prepareStatement("insert into userinfo values(?,?,?,?,?,?)");
prepare.setString(1,ID);
prepare.setString(2,password);
prepare.setString(3,name);
prepare.setString(4,sex);
prepare.setLong(5,getDate(date));prepare.setString(6,introduce);
prepare.executeUpdate();
session.setAttribute("user",ID);
response.sendRedirect("index.jsp");
}
catch(Exception e)
{
response.sendRedirect("register.jsp");
}finally
{if(prepare!=null){
try{prepare.close();}catch(Exception e){e.printStackTrace();}
}
if(con!=null){
try{con.close();}catch(Exception e){e.printStackTrace();}
}
}
%>
乱码问题如果大家嫌麻烦我会自己去看的 主要就是好像register页面传到数据库里的数据和我想要的不一致问题怎么解决 谢谢大家
{
if(rs.getString("password").equals(password))
{
response.sendRedirect("index.jsp");
session.setAttribute("user",ID);
}
是while(rs.next())
而且把request.getparameter的东西system.out.println一下
如果是乱码的话new string(username.getBytes("iso8859-1"),gb2312));一下
{
if(rs.getString("password").equals(password))
{
response.sendRedirect("index.jsp");
session.setAttribute("user",ID);
}
楼上已经说这个了,用if的话只读了数据库的第一条记录,如果你注册的用户信息插入到了后面你就读不到了。关于乱码
String sex = request.getParameter("sex");
改为:
String sex = new String(request.getParameter("sex").getBytes(
"iso-8859-1"), "utf-8"));
用户从浏览器提交的数据一般是按照iso8859-1 编码的。而我们显示汉字一般常用的编码是:gb2312,gbk,utf-8 。所以需要转换。如果你觉得每个接收参数都这么写很麻烦的话可以写一个过滤器。csdn就有人写过,链接就不发了。数据库,tomcat,jsp ,这三个你要把编码方式设成相同的。我个人喜欢统一用utf-8.
打开IE -> 工具->Internet选项->高级->拖到最后,将"总是以UTF-8发送URL"取消选择即可,你页面的编码还是gb2312不改.
<%request.setCharacterEncoding("gb2312");%>试试!