我有一个登录页面,它连接到数据库以通过servlet进行验证。 成功登录后,我在servlet中使用请求调度程序登录success.html
这是servlet代码:
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.RequestDispatcher;
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.servlet.http.HttpSession;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
/**
* Servlet implementation class DoLogin
*/
@WebServlet("/DoLogin")
public class DoLogin extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public DoLogin() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
PrintWriter out = response.getWriter();
response.setContentType("text/html");
out.println("<html>");
//Browser Back Button Disable Script After Logout
out.println("<script>");
out.println("javascript:window.history.forward(1)");
out.println("</script>");
out.println("<body style=\"background-color : rgb(41,85,153);\">");
//Invalidate The Session.
HttpSession session = request.getSession(false);
try
{
session.invalidate();
out.println("<h3><font family=\"Times New Roman\" color=\"white\">Successfully Logout</font></h3>");
out.println("<form action=\"/CloudMoV/login.html\" method=\"post\">");
out.println("<br />");
out.println("<input type=\"submit\" alt=\"submit\" value=\"Login Again!\"/>");
out.println("</form>");
out.println("</body>");
out.println("</html>");
}
catch (Exception ee)
{
out.println(ee);
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setHeader("Cache-Control", "private, no-store, no-cache, must-revalidate");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
// out.println("<html>");
// out.println("<head>");
// out.println("<title>");
// out.println("<html>");
// out.println("</title>");
// out.println("</head>");
// out.println("<body style=\"background-color : black;\">");
// connecting to database
Connection con = null;
Statement stmt = null;
ResultSet res = null;
//variable for checking the values true and false.
boolean success=false;
//creating session
HttpSession session = request.getSession(true);
try{
DbConnection db = new DbConnection();
con = db.connDb();
//Receiving Values From HTML FORM
String thisname=request.getParameter("username");
String thispwd=request.getParameter("password");
stmt = (Statement) con.createStatement();
//Query for selecting user name and password from database.
String q = "select username, password from register where username='"+thisname+"'";
res = stmt.executeQuery(q);
while(res.next())
{
if ((thisname.equals(res.getString("username"))) && (thispwd.equals(res.getString("password"))))
{
// out.println("<br /><br /><h3><font family=\"Times New Roman\" color=\"white\">You're successfully logged in. <br />Welcome "+res.getString("username")+"</font></h3>");
success = true;
}
}
}
catch (SQLException e)
{
throw new ServletException("Servlet Could not display records.", e);
}
catch (Exception e)
{
throw new ServletException("Exception.", e);
}
// if(success==true)
// {
// out.println("<font color=\"white\">session Id: " + session.getId() + "</font><br><br>");
// session.setAttribute("username", request.getParameter("username"));
//
// //Logout Button
// out.println("<form onclick=\"doGet("+request+","+ response+")\" >");
// out.println("<input type=\"submit\" value=\"logout\">");
// out.println("</form>");
// }
if(success==true)
{
session.setAttribute("username", request.getParameter("username"));
RequestDispatcher rd = request.getRequestDispatcher("/login-success.html");
rd.forward(request, response);
}
if(success==false)
{
// out.println("<br /><h3 style=\"text-align:center;color:#FFFFFF;margin-top:150px\">Invalid User Name or Password</h3>");
// out.println("<br /><a href=\"login.html\"><h3 style=\"text-align:center;color:#FFFFFF;text-decoration:underline\">Go Back</h3></a>");
session.invalidate();
RequestDispatcher rd = request.getRequestDispatcher("/login.html");
rd.forward(request, response);
}
//
// out.println("</body>");
// out.println("</html>");
}
}
在登录成功中我想显示用户名。我该怎么做? 我相信我应该首先将login-success.html更改为login-success.jsp
答案 0 :(得分:1)
您正在将用户名值设置为会话属性“username”,因此您可以使用此EL表达式$ {sessionScope.username} 在你的jsp中。你应该在jstl标签
中使用它答案 1 :(得分:0)
创建http sesson并使用用户信息推送登录dto并从任何jsp页面访问会话变量并检索用户/登录信息。
答案 2 :(得分:0)
身份验证成功后,在会话中设置用户名属性
session.setAttribute("loggedInUser_userName", userName);
由于HTML是静态页面,因此需要ajax调用来更新页面,因此优先选择HTML而不是JSP。
使用El Expression ${}
打印用户名。 El Expression不需要JSTL标签库。
在您的用户名范围内
<span>${loggedInUser_userName}</span>