欢迎消息jsp java

qoefvg9y  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(291)

我已经创建了一个简单的web应用程序,其中有人可以注册e登录。现在有人在日志中显示“欢迎”现在这是代码:
登录.java
我添加了一个logged-in来检查是否有人登录了这个站点,但我认为这是不对的。

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package hotel;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.servlet.RequestDispatcher;
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 Login extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        try {

            String s1 = request.getParameter("t1");
            String s2 = request.getParameter("t2");
            out.print("Welcome "+ s1);

            Class.forName("com.mysql.jdbc.Driver");
            out.println("driver loaded");
            Connection  con = DriverManager.getConnection("jdbc:mysql://localhost:3306/HotelReservation","root" ,"123456789");
            out.println("Connect");
            Statement  st =  con.createStatement();
            out.println("conncetion successfull");

                ResultSet rs =   st.executeQuery("select email,uid, pass from register where email = '"+s1+"' and pass = '"+s2+"'");
                rs.next();    
                String cid = rs.getString("uid");
                out.println("recod is inserted" +cid);

          HttpSession session=request.getSession();  
          session.setAttribute("sessname",cid);
         // out.println("<a href=\"LoginSucess.jsp\">Check Output Page Here </a>");
          session.setAttribute("logged-in","true");

          String redirectedPage = "/parentPage";
          response.sendRedirect("loginsuccess.jsp");

        } catch (Exception e) {
            out.println("Driver not found" +e);
        }
        finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}

我想把这个信息放在导航菜单上,你知道我该怎么做吗?例如,我创建了:

<% String name="";
    if(session.getAttribute("Logged-in") != null) {
    name = (String)session.getAttribute("name");
} else {
    // response.sendRedirect("login.jsp");
}

%>
<div id="container">
<div id="nav">       
        <a href="home.jsp">Home</a>
        <a href="hotel.jsp">Hotel</a>
        <a href="login.jsp">Login</a>
        <a href="registration.jsp">Registrazione</a>
        Welcome <%= name %>
</div>

但我只收到“欢迎”。谢谢你

gz5pxeao

gz5pxeao1#

您无法存储登录凭据的信息。尝试将其存储到控制器中或将值提取到servlet中并使用它。
或者。。。
您将会话初始化为“已登录”,并将其用作“已登录”。尝试将其更改为区分大小写。->session.setattribute(“已登录”,“true”);
-->if(session.getattribute(“已登录”)!=(空){

4ioopgfo

4ioopgfo2#

Your code is not working because some error are there at in your code. here this is your code.

<% String name="";
    if(session.getAttribute("Logged-in") != null)
    {
       name = (String)session.getAttribute("name");
    }
    else 
    {
      // response.sendRedirect("login.jsp");
    }

%>

now in this your code "name" parameter not set in your controller side. so if you simply write this code in your controller side then your problem is solved.

 session.setAttribute("name",cid);

put this code in your controller side and again run your project.

相关问题