我开始尝试使用JSP和Servlets。我有一个项目可以让你创建一个帐户并在Oracle db中保存用户数据。我创建了一个允许用户快速登录的表单,使用电子邮件和密码字段的webapps标题:
<jsp:include page="/includes/header.jsp" />
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<link href="<c:url value='sources/css/logIn.css'/>" type="text/css" rel="stylesheet" />
<form action="logIn" method="post">
<table>
<td>
<div id="header_logo">
<a href="<c:url value='/welcome.jsp'/>">
<img src="<c:url value='/sources/images/logo.jpg'/>">
</a>
</div>
</td>
<td>
Dont have an account?
</td>
<td>
<a href ="create_account.jsp"> JOIN! </a></td>
<td class="header_label" align="left">
<div id ="header_email">
email Address<br>
<input type="text" name="logEmailAddress" size="15" value="" class="header_input">
</div>
</td>
<td class="header_label" align="left">
<div id="header_password">
password<br>
<input type="password" name="logPassword" size="15" value="" class="header_input">
</div>
</td>
<td>
<div id="header_submit">
<br><input id="sub" type="submit" value="Log In">
</div>
</td>
</table>
</form>
我使用post方法调用LogInServlet,如下所示:
package appUsers;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import business.User;
import data.UserDB;
public class LogInServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
String logEmailAddress = request.getParameter("logEmailAddress");
String logPassword = request.getParameter("logPassword");
String url = "";
String message ="";
boolean isLogged=false;
//debug message
System.out.println("user input is emailaddress: " + logEmailAddress + " and password: " + logPassword);
if (logEmailAddress.length()==0 || logPassword.length()==0)
{
message = "Please provide your email address and password";
url = "/welcome.jsp";
}
User user = UserDB.selectUser(logEmailAddress);
if (user!=null)
{
isLogged = logPassword.equals(user.getPassword());
System.out.println(isLogged);
if(isLogged)
{
message = "You have succesfully loged in";
url = "/welcome.jsp";
}
else
{
message = "Invalid password";
url = "/welcome.jsp";
}
}
else
{
message = "No such account";
url = "/welcome.jsp";
}
HttpSession session = request.getSession();
session.setAttribute("user", user);
session.setAttribute("isLogged", isLogged);
request.setAttribute("message",message);
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);
dispatcher.forward(request, response);
}
}
从db中选择用户的函数:
User user = UserDB.selectUser(logEmailAddress);
实现如下:
public static User selectUser(String emailAddress)
{
ConnectionPool pool = ConnectionPool.getInstance();
Connection connection = pool.getConnection();
PreparedStatement ps = null;
ResultSet rs = null;
String query = "SELECT * FROM USERS " + "WHERE EmailAddress = ?";
try
{
ps = connection.prepareStatement(query);
ps.setString(1, emailAddress);
rs = ps.executeQuery();
User user = null;
if (rs.next())
{
user = new User();
user.setFirstName(rs.getString("FirstName"));
user.setLastName(rs.getString("LastName"));
user.setEmailAddress(rs.getString("EmailAddress"));
user.setUserName(rs.getString("UserName"));
user.setPassword(rs.getString("Password"));
user.setUserType(rs.getString("Type"));
}
return user;
}
catch (SQLException e){
e.printStackTrace();
return null;
}
finally
{
DBUtil.closeResultSet(rs);
DBUtil.closePreparedStatement(ps);
pool.freeConnection(connection);
}
}
以上所有工作都很好,用户从数据库中选择并在从标题字段登录时确认用户帐户。
昨天我试图创建一个类似的表单,它显示在主视图中,使用post方法调用相同的Servlet:
<jsp:include page="/includes/header.jsp" />
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<link href="<c:url value='sources/css/logIn.css'/>" type="text/css" rel="stylesheet" />
<div id="log">
<h1>Log in to Triptip</h1>
<form action="logIn" method="post">
<table>
<tr class="spaceBetweenRows">
<td class="user_label" align="left">
email Address<br>
</td>
<td>
<input class="user_input" type="text" name="logEmailAddress" value=" ">
</td>
</tr>
<tr>
<td class="user_label" align="left">
password<br>
</td>
<td>
<input class="user_input" type="password" name="logPassword" value="">
</td>
</tr>
<tr>
<td>
</td>
<td align="right">
<br>
<input id="sub" type="submit" value="Log In">
</td>
</tr>
</table>
</form>
</div>
</body>
虽然我已经确认属性Email和密码正在正确地传递给Servlet,但用户user = UserDB.selectUser(logEmailAddress);尽管用户存在,但返回null!
任何人都知道可能会发生什么?我尝试实现一个新的servlet,但结果相同!任何帮助都会受到赞赏,因为我花了很多时间试图弄清楚这个问题!
我使用的是Windows 7 64,Netbeans,7.4 Java 1.7.0-51,Tomcat 7和Oracle 11.2XE
答案 0 :(得分:0)
在您的代码中,您设置一个包含空格的默认值 -
<input class="user_input" type="text" name="logEmailAddress" value=" ">
根据您的评论,您没有注意到这一点,只是添加了更多字符。您不需要默认值,因此可能是,
<input class="user_input" type="text" name="logEmailAddress" value="">
或只是
<input class="user_input" type="text" name="logEmailAddress">
答案 1 :(得分:0)
正如您指定的那样
<input class="user_input" type="text" name="logEmailAddress" value=" ">
所以每当你在文本字段中输入一些值,如abc
和
String email = request.getParameter("logEmailAddress");
电子邮件应该给你initialspace+abc
所以改变
<input class="user_input" type="text" name="logEmailAddress" value=" ">
到
<input class="user_input" type="text" name="logEmailAddress" value="">