欢迎消息JSP JAVA

时间:2018-07-11 11:00:52

标签: java mysql jsp

我已经创建了一个简单的Web应用程序,有人可以在其中注册电子登录。 现在有人要登录的地方出现“欢迎使用” 现在,这是以下代码:

  

login.java

我添加了一个登录名,以检查是否有人登录了该网站,但我认为这是不对的。

/*
 * 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>

但是我只收到:“欢迎”。 谢谢

2 个答案:

答案 0 :(得分:0)

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.

答案 1 :(得分:0)

您无处存储登录凭据的信息。尝试将其存储到控制器中或将值获取到servlet中并使用它。

或者...

您将会话初始化为“已登录”,并将其用作“已登录”。尝试将其更改为区分大小写。  -> session.setAttribute(“ logged-in”,“ true”);

-> if(session.getAttribute(“ Logged-in”)!= null){