forkert login side bliver vist.
Hejsa.Jeg har fået lavet mig en login form, som tjekker om der er en bruger i databasen. Hvis denne bruger findes, skulle man komme hen til en side, hvor man får at vide man er logget ind. Problematikken er at jeg kommer hen til den side hvor man får at vide "user is invalid". Jeg har tjekket mange gange at det er de andre informationer jeg putter ind, i forhold til hvad jeg har i min database. Noget siger mig at servleten ikke forbinder korrekt til databasen, da jeg ellers skulle få beskeden "Sorry, you are not a registered user! Please sign up first", hvis jeg puttede noget tilfædigt ind. Min JSP og servlet + xml er her: Håber nogle har en ide til hvad der går galt? Som et tillægsspørgsmål vil jeg gerne lave annotation istedet for web.xml.
Mvh Mads
JSP:
package ExamplePackage;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LoginServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException {
try {
UserBean user = new UserBean();
user.setUserName(request.getParameter("un"));
user.setPassword(request.getParameter("pw"));
user = UserDAO.login(user);
if (user.isValid()) {
HttpSession session = request.getSession(true);
session.setAttribute("currentSessionUser",user);
response.sendRedirect("userLogged.jsp"); //logged-in page
}
else
response.sendRedirect("invalidLogin.jsp"); //error page
}
catch (Throwable theException) {
System.out.println(theException);
}
}
}
UserBean:
package ExamplePackage;
public class UserBean {
private String username;
private String password;
private String firstName;
private String lastName;
public boolean valid;
public String getFirstName() {
return firstName;
}
public void setFirstName(String newFirstName) {
firstName = newFirstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String newLastName) {
lastName = newLastName;
}
public String getPassword() {
return password;
}
public void setPassword(String newPassword) {
password = newPassword;
}
public String getUsername() {
return username;
}
public void setUserName(String newUsername) {
username = newUsername;
}
public boolean isValid() {
return valid;
}
public void setValid(boolean newValid) {
valid = newValid;
}
}
UserDAO:
package ExamplePackage;
import java.text.*;
import java.util.*;
import java.sql.*;
public class UserDAO
{
static Connection currentCon = null;
static ResultSet rs = null;
public static UserBean login(UserBean bean) {
//preparing some objects for connection
Statement stmt = null;
String username = bean.getUsername();
String password = bean.getPassword();
String searchQuery =
"SELECT * FROM users WHERE username='"
+ username
+ "' AND password='"
+ password
+ "'";
// "System.out.println" prints in the console; Normally used to trace the process
System.out.println("Your user name is " + username);
System.out.println("Your password is " + password);
System.out.println("Query: "+searchQuery);
try
{
//connect to DB
currentCon = ConnectionManager.getConnection();
stmt=currentCon.createStatement();
rs = stmt.executeQuery(searchQuery);
boolean more = rs.next();
// if user does not exist set the isValid variable to false
if (!more)
{
System.out.println("Sorry, you are not a registered user! Please sign up first");
bean.setValid(false);
}
//if user exists set the isValid variable to true
else if (more)
{
String firstName = rs.getString("FirstName");
String lastName = rs.getString("LastName");
System.out.println("Welcome " + firstName);
bean.setFirstName(firstName);
bean.setLastName(lastName);
bean.setValid(true);
}
}
catch (Exception ex)
{
System.out.println("Log In failed: An Exception has occurred! " + ex);
}
//some exception handling
finally
{
if (rs != null) {
try {
rs.close();
} catch (Exception e) {}
rs = null;
}
if (stmt != null) {
try {
stmt.close();
} catch (Exception e) {}
stmt = null;
}
if (currentCon != null) {
try {
currentCon.close();
} catch (Exception e) {
}
currentCon = null;
}
}
return bean;
}
}
ConnectionManager:
package ExamplePackage;
import java.sql.*;
import java.util.*;
public class ConnectionManager {
static Connection con;
static String url;
public static Connection getConnection()
{
try
{
String url = "jdbc:mysql://localhost/ConnectionLoginFormDB" + "root" + "";
// assuming "DataSource" is your DataSource name
Class.forName("com.mysql.jdbc.Driver");
try
{
con = DriverManager.getConnection(url,"username","password");
// assuming your SQL Server's username is "username"
// and password is "password"
}
catch (SQLException ex)
{
ex.printStackTrace();
}
}
catch(ClassNotFoundException e)
{
System.out.println(e);
}
return con;
}
}
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>ConnectionLoginForm</display-name>
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>ExamplePackage.LoginServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/LoginServlet</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>UserBean</servlet-name>
<servlet-class>ExamplePackage.UserBean</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UserBean</servlet-name>
<url-pattern>/UserBeanpath</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>UserDAO</servlet-name>
<servlet-class>ExamplePackage.UserDAO</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UserDAO</servlet-name>
<url-pattern>/UserDAO</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>ConnectionManager</servlet-name>
<servlet-class>ExamplePackage.ConnectionManager</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ConnectionManager</servlet-name>
<url-pattern>/ConnectionManager</url-pattern>
</servlet-mapping>
</web-app>