欢天喜地! 刚做完登录模块。遇到一些问题! 都放在里面了!
话不多说,放代码!!!
login.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ include file="head.txt" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录</title>
</head>
<body background=image/login.jpg ><font size=2>
<div align="center">
<form action="LoginServlet" method="post" name="form">
<table>
<h1 style="color:red"> 用户名字由数字,字母,下划线构成,*注释的项必须填写!</h1>
<tr><td>*用户名称:<input type=text name="UserName"></td>
<td>*用户密码:<input type=password name="PassWord"></td>
<td><input type=submit name="g" value="提交"></td></tr>
</table>
</form>
</div>
<jsp:useBean id="loginbean" class="com.mybean.LoginBean" scope="session" />
<center><h1><jsp:getProperty name="loginbean" property="backNews" /></h1></center>
</body>
</html>
```loginbean
/**
*
*/
package com.mybean;
/**
* @autho Xran
*
*/
public class LoginBean {
public String UserName="",
PassWord="";
public String backNews="";
/**
* @return backNews
*/
public String getBackNews() {
return backNews;
}
/**
* @param backNews 要设置的 backNews
*/
public void setBackNews(String backNews) {
this.backNews = backNews;
}
/**
* @return userName
*/
public String getUserName() {
return UserName;
}
/**
* @return passWord
*/
public String getPassWord() {
return PassWord;
}
}
**loginservlet**
/**
*
*/
package com.myservlet;
import com.mybean.*;
import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
* @autho Xran
*
*/
public class LoginServlet extends HttpServlet {
public void init(ServletConfig config)throws ServletException{
super.init(config);
try { Class.forName("com.mysql.cj.jdbc.Driver");
}
catch(Exception e) {}
}
public String LoginString(String s) {
try {byte bb[]=s.getBytes("UTF-8");
s=new String(bb);
}
catch(Exception ee) {}
return s;
}
String uri="jdbc:mysql://localhost/bookstore?user=root&password=123&characterEncoding=UTF-8&serverTimezone=GMT";
Connection con;
ResultSet rs;
PreparedStatement sql;
Statement stat;
LoginBean loginbean =new LoginBean();
/**
* 连接测试
*/
// public static void main(String[] args) {
// LoginServlet lst=new LoginServlet();
// lst.conn();
//
// }
public void conn() {
try {
con=DriverManager.getConnection(uri);
//System.out.println("连接成功!");
stat=con.createStatement();
String cond="select * from book ;";
ResultSet m=stat.executeQuery(cond);
while(m.next()) {
//String name;
System.out.println(m.getString(1));
}
//System.out.println(m);
System.out.println("连接成功!");
} catch (SQLException e) {
// TODO 自动生成的 catch 块
System.out.println("连接错误"+e.getMessage());
}
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response); }
public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
HttpSession session=request.getSession(true);
session.setAttribute("loginbean", loginbean);
String UserName=request.getParameter("UserName").trim();
String PassWord=request.getParameter("PassWord").trim();
String backNews="";
if(UserName==null&PassWord==null) {
UserName="";
PassWord="";
}
boolean isLD=true;
for(int i=0;i<UserName.length();i++) {
char c=UserName.charAt(i);
if(!((c<='z'&&c>='a')||(c<='Z'&&c>='A')||(c<='9'&&c>='0')))
isLD=false;
}
boolean boo=(UserName.length()>0)&&(PassWord.length()>0)&&isLD;
try {
con=DriverManager.getConnection(uri);
String condition="select * from user where UserName ='" +UserName +"'and PassWord='"+PassWord+"'";
stat=con.createStatement();
// sql=con.prepareStatement(condition); //预处理
if(boo) {
// sql.setString(1,LoginString(UserName));
// sql.setString(2,LoginString(PassWord));
// rs = sql.executeQuery(condition);
rs=stat.executeQuery(condition);
boolean m=rs.next();
if(m==true) {
backNews="登录成功";
loginbean.setBackNews(backNews);
//response.sendRedirect("login.jsp");
}else {
backNews="用户名不存在或者密码不匹配";
loginbean.setBackNews(backNews);
}
}
else {
backNews="用户名不存在或者密码不匹配";
loginbean.setBackNews(backNews);
// RequestDispatcher dis=request.getRequestDispatcher("regist.jsp");
// dis.forward(request, response);
}
con.close();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
backNews=""+e;
loginbean.setBackNews(backNews);
}
RequestDispatcher dis=request.getRequestDispatcher("login.jsp");
dis.forward(request, response);
}
}
之前的错误:SQLException: Parameter index out of range (1 > number of parameters, which is 0).
原因分析: 我之前用的是预处理语句:String condition ="select * from user where UserName='?' and PassWord='?' "
之后使用sql.setString(1,LoginString(UserName));
给定通配符意义。
估摸着是这个的原因。
后来改用 Statement stat 就可以用了!!!!