[英]make a simple login authentication using java class,servlet,jsp and session and database
I want to make a login process using mvc structure where i send userid and password from index.jsp
page.If they match with the database value then it will go to the stater001.jsp
.In index.jsp
i've 2 fields userid and password from.I've post userid and pwd into LoginServlet
servlet. 我想使用mvc结构进行登录过程,其中我从index.jsp
页面发送用户名和密码。如果它们与数据库值匹配,那么它将进入stater001.jsp
。在index.jsp
我有2个字段userid和密码来自。我已将userid和pwd发布到LoginServlet
servlet中。
LoginServlet
process userid , pwd
making the instance of loginAuthentication
class. LoginServlet
进程userid , pwd
成为loginAuthentication
类的实例。 loginAuthentication
class checks whether userid
and password
exists in database or not with select query
.I have also used setter getter class user
to set userid
and pwd
into session.I tried to do the whole login process in an organised way.But i'm stucked with error i couldn't under stand what is the problem please help me to solve this.First i tried it without using mvc pattern then it worked fine.But now it is not working. loginAuthentication
类通过select query
检查数据库中是否存在userid
和password
。我也使用了setter getter类user
将userid
和pwd
设置为session。我试图以一种有组织的方式完成整个登录过程。出现错误,我无法理解是什么问题,请帮助我解决此问题。首先我没有使用mvc模式就尝试了它,然后它正常工作。但是现在不起作用了。 Below is my previous code all process in one servlet 下面是我以前的代码,所有过程都在一个servlet中进行
package DataAccess;
import DataAccess.dbconnector;
import java.io.IOException;
import java.io.PrintWriter;
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 java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
@WebServlet(urlPatterns = {"/LoginServlet"})
public class LoginServlet extends HttpServlet {
String userid, pwd;
Connection connection = null;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
Statement statement = null;
userid = request.getParameter("uname");
pwd = request.getParameter("pass");
try {
dbconnector dbc = new dbconnector();
connection = dbc.Open();
PreparedStatement ps = connection.prepareStatement("select * from member where uname='" + userid + "' and pass='" + pwd + "'");
ResultSet rs = ps.executeQuery();
if (rs.next()) {
HttpSession session = request.getSession();
session.setAttribute("userid", userid);
response.sendRedirect("view/starter001.jsp");
} else {
request.setAttribute("errorMessage", "Invalid user or password");
RequestDispatcher rd = getServletContext().getRequestDispatcher("/index.jsp");
rd.include(request, response);
}
} catch (Exception e) {
e.printStackTrace();
}
finally {
try {
connection.close();
} catch (SQLException ex) {
Logger.getLogger(LoginServlet.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
} }
And in my new code here i've stucked with error in execution 在我的新代码中,我一直卡在执行错误中 And here i've attached my new edited codes where i tried to do this in an organised way .My errors have been gone but not showing the userid after login http://pastebin.com/RKyieMqn Please consider my question 在这里,我附加了新编辑的代码,尝试以一种有组织的方式进行操作。我的错误已消失,但登录http://pastebin.com/RKyieMqn后未显示用户ID请考虑我的问题
at first when all codes were in loginservlet then it was like 首先,当所有代码都在loginservlet中时, but when i start using mvc structure it is not showing the username 但是当我开始使用MVC结构时,它没有显示用户名
Obviously, you pasted the wrong code: In the below statement, where is the variable user defined? 显然,您粘贴了错误的代码:在以下语句中,用户定义的变量在哪里? session.setAttribute("user", user); session.setAttribute(“ user”,user);
Perhaps, you have created a authenticateUser object somewhere. 也许您已经在某个地方创建了一个authenticateUser对象。 Please check you code to find it. 请检查您的代码以找到它。
I have changed session.setAttribute("userid", user);
我已经更改了session.setAttribute("userid", user);
to session.setAttribute("userid", user.getUserId());
到session.setAttribute("userid", user.getUserId());
and now it's working 现在它正在工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.