亲爱的,
我正在使用oracle数据库。用户可以注册该应用程序,也可以登录。
注册和登录功能均在控制器的post方法中按个别方式工作。但是,如果我将其放在一起,它将无法正常工作。
Controller.java
package controller;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
/**
* Servlet implementation class Controller
*/
@WebServlet("/Controller")
public class Controller extends HttpServlet {
private static final long serialVersionUID = 1L;
private DataSource ds;
public void init() throws ServletException {
try {
InitialContext ic = new InitialContext();
ds = (DataSource) ic.lookup("java:comp/env/jdbc/myoracle");
} catch (Exception e) {
throw new ServletException(e);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String action = request.getParameter("action");
String address = null;
request.setAttribute("email", "");
request.setAttribute("password", "");
request.setAttribute("loginerror", "");
request.setAttribute("signupMessage", "");
if (action == null) {
address = "/login.jsp";
}
else if (action.equals("signup")) {
address = "/signup.jsp";
}
request.getServletContext().getRequestDispatcher(address).forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Connection con = null;
try {
con = ds.getConnection();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String loginValidate = request.getParameter("loginValidate");
String signupValidation = request.getParameter("signupValidation");
//Validate Sign up
if (signupValidation.equals("yes")) {
userHandle user = new userHandle(con);
String email = request.getParameter("emailsignup");
String password = request.getParameter("passwordsignup");
try {
if (user.signupValidation(email, password)) {
request.setAttribute("signupMessage", "Succesfully Logged in...");
request.getServletContext().getRequestDispatcher("/success.jsp").forward(request, response);
} else {
request.setAttribute("signupMessage", "Invalid User/Name and password!!");
request.getServletContext().getRequestDispatcher("/signup.jsp").forward(request, response);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//Validate login.
else if (loginValidate.equals("yes")) {
userHandle user1 = new userHandle(con);
String e = request.getParameter("email");
String p = request.getParameter("password");
try {
if (user1.userValidate(e, p)) {
request.setAttribute("loginerror", "Scucess");
request.getServletContext().getRequestDispatcher("/success.jsp").forward(request, response);
} else {
request.setAttribute("loginerror", "Invalid User Name and password!!");
request.getServletContext().getRequestDispatcher("/login.jsp").forward(request, response);
}
} catch (SQLException e1) {
e1.printStackTrace();
}
}
try {
con.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
但是,如果我在控制器中注释了“验证注册”部分,但登录功能无法正常工作。
带有注释的“验证的注册”部分的Controller.java:
package controller;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
/**
* Servlet implementation class Controller
*/
@WebServlet("/Controller")
public class Controller extends HttpServlet {
private static final long serialVersionUID = 1L;
private DataSource ds;
public void init() throws ServletException {
try {
InitialContext ic = new InitialContext();
ds = (DataSource) ic.lookup("java:comp/env/jdbc/myoracle");
} catch (Exception e) {
throw new ServletException(e);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String action = request.getParameter("action");
String address = null;
request.setAttribute("email", "");
request.setAttribute("password", "");
request.setAttribute("loginerror", "");
request.setAttribute("signupMessage", "");
if (action == null) {
address = "/login.jsp";
}
else if (action.equals("signup")) {
address = "/signup.jsp";
}
request.getServletContext().getRequestDispatcher(address).forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Connection con = null;
try {
con = ds.getConnection();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String loginValidate = request.getParameter("loginValidate");
String signupValidation = request.getParameter("signupValidation");
//Validate Sign up
// if (signupValidation.equals("yes")) {
// userHandle user = new userHandle(con);
// String email = request.getParameter("emailsignup");
// String password = request.getParameter("passwordsignup");
//
// try {
// if (user.signupValidation(email, password)) {
// request.setAttribute("signupMessage", "Succesfully Logged in...");
// request.getServletContext().getRequestDispatcher("/success.jsp").forward(request, response);
// } else {
// request.setAttribute("signupMessage", "Invalid User/Name and password!!");
// request.getServletContext().getRequestDispatcher("/signup.jsp").forward(request, response);
//
// }
// } catch (SQLException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
//
// }
//
//Validate login.
if (loginValidate.equals("yes")) {
userHandle user1 = new userHandle(con);
String e = request.getParameter("email");
String p = request.getParameter("password");
try {
if (user1.userValidate(e, p)) {
request.setAttribute("loginerror", "Scucess");
request.getServletContext().getRequestDispatcher("/success.jsp").forward(request, response);
} else {
request.setAttribute("loginerror", "Invalid User Name and password!!");
request.getServletContext().getRequestDispatcher("/login.jsp").forward(request, response);
}
} catch (SQLException e1) {
e1.printStackTrace();
}
}
try {
con.close();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
可以帮助我为什么会这样吗?我在这里做错了什么?非常感谢您的宝贵时间和反馈。
Login.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Login</title>
</head>
<body>
<form action="/db/Controller" method="post">
Email: <input type="text" name="email" value="<%=request.getAttribute("email")%>"/>
<br>
Password: <input type="text" name="password" value="<%=request.getAttribute("password")%>"/>
<br>
<input type="submit" value="Login" />
<input type="hidden" name="loginValidate" value="yes"/>
<br>
</form>
<h2><%= request.getAttribute("loginerror") %></h2>
<br>
<a href="/db/Controller?action=signup"> Click here to sign up... </a>
</body>
</html>
userHandler.java
package controller;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class userHandle {
private String email;
private String password;
private Connection con;
public userHandle() {
}
public userHandle(Connection con) {
this.con = con;
}
public boolean userValidate(String email, String password) throws SQLException {
String sql = "select count(*) as count from users where email = ? and password = ?";
PreparedStatement stmt = con.prepareStatement(sql);
stmt.setString(1, email);
stmt.setString(2, password);
ResultSet rs = stmt.executeQuery();
int count = 0;
if(rs.next()) {
count=rs.getInt("count");
}
if( count ==0) {
return false;
}
return true;
}
public boolean exists(String email) throws SQLException {
String sql = "select count(*) as count from users where email = ?";
PreparedStatement stmt = con.prepareStatement(sql);
stmt.setString(1, email);
ResultSet rs = stmt.executeQuery();
int count = 0;
if(rs.next()) {
count=rs.getInt("count");
}
stmt.close();
if( count ==0) {
return false;
}
return true;
}
public boolean signupValidation(String email, String password) throws SQLException {
if (exists(email)){
return false;
}
else {
String sql = "insert into users values(?,?)";
PreparedStatement stmt = con.prepareStatement(sql);
stmt.setString(1, email);
stmt.setString(2, password);
int count = stmt.executeUpdate();
if(count == 0 ) {
return false;
}
}
return true;
}
}
success.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Logged in...
</body>
</html>
signup.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Sign up...</title>
</head>
<body>
<h1> Welcome to the Club. Sign up for free!!!!</h1>
<form action="/db/Controller" method="post">
<br>
Email: <input type="text" name="emailsignup"/>
<br>
password: <input type="text" name="passwordsignup"/>
<br>
<input type="hidden" value="yes" name="signupValidation"/>
<input type="submit" value="Submit" />
</form>
<h2><%=request.getAttribute("signupMessage") %></h2>
<a href="/db/Controller" >Click here to log in...</a>
</body>
</html>
<form action="/db/Controller" method="post">
Email: <input type="text" name="email" value="<%=request.getAttribute("email")%>"/>
<br>
Password: <input type="text" name="password" value="<%=request.getAttribute("password")%>"/>
<br>
<input type="submit" value="Login" />
<input type="hidden" name="loginValidate" value="yes"/>
<input type="hidden" name="signupValidation" value="no"/>
<br>
</form>
类似地,您需要在"loginValidate"
中设置缺少的参数signup.jsp
。
而且,在控制器中将一些已知常量与您的请求参数进行比较时,您可以避免使用NullPointerException
:
if ("yes".equals(signupValidation)) { // no NPE here if signupValidation is null // ... the rest of your code }