简体   繁体   中英

Invoking a JSP Page from a Servlet

Can anyone tell me if there exists any method other than RequestDispatcher, to invoke a jsp page from my servlet? Because i have tried a lot without success.

My servlet works normally and recovred all the data from jsp. All that I need is to be redirected to another page when the user enters the username and password correctly.

my code :

first my servlet " login"

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

}


  protected void doGet(HttpServletRequest request, HttpServletResponse      response)
        throws ServletException, IOException {
    processRequest(request, response);
}


    protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    try {
        String user = request.getParameter("username");
        String pass = request.getParameter("password");         
      System.out.println(" le user est "+user+ " le mot de passe est " + pass);

          String query = "SELECT * FROM users WHERE username = '"+user+"' and password='"+pass+"'";

      dbconn = new DBAccess();
      Connection conn = dbconn.connect();

         stmt = conn.createStatement();
         ResultSet res = stmt.executeQuery(query);

            if(res.next()){
              ServletContext sc = this.getServletContext();
              RequestDispatcher rd =sc.getRequestDispatcher( "inscreption.jsp");
              rd.forward(request, response);   
                 System.out.println(" il existe");

            }else { 
           ServletContext sc = this.getServletContext();
            RequestDispatcher rd = sc.getRequestDispatcher("index.jsp");
             //   RequestDispatcher rd =request.getRequestDispatcher("index.jsp");
                rd.forward(request, response);   
                System.out.println("not found");


      }
    } catch (SQLException ex) {
        Logger.getLogger(login.class.getName()).log(Level.SEVERE, null, ex);
    }


    finally{
        System.out.close();
    }





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

my jsp that i would to be redirected for

<html>
<head>
    <!--local jquery-->
    <script  src="jQuerys/jquery-1.9.1.min.js"></script>
    <script  src="jQuerys/jquery.mobile-1.3.1.min.js"></script>
    <link rel ="stylesheet" type="text/css" href="css/jqueryMobile-1.3.1.css"/>
    <!--/local jquery-->
      </head>

      <body>
<div data-role="page" id="inscription">
<div data-role="header" data-theme="b">
    <center>CERIST</center>
</div>
                  <form id="insc" method="post" action="login">

  <div data-role="content">
    <div data-role="fieldcontain">
        <label for="identifiant">Identifiant </label>
        <input type="text" id="identifiant"/>
    </div>
    <div data-role="fieldcontain">
        <label for="password1">Mot de passe </label>
        <input type="password" id="password1"/>
    </div>
    <div data-role="fieldcontain">
        <label for="password2">Confirmation</label>
        <input type="password" id="password2"/>
    </div>
    <div data-role="fieldcontain">
        <fieldset data-role="controlgroup">
            <legend>Vous &ecirc;tes :</legend>
                <input type="radio" name="profil" id="radio-choice-1" value="Candidat" />
                <label for="radio-choice-1">Utilisateur</label>

                <input type="radio" name="profil" id="radio-choice-2" value="Entreprise"  />
                <label for="radio-choice-2">Administrateur</label>
        </fieldset>
    </div>
    <br/>
    <a href="#" data-role="button" onclick="">S'inscrire</a>

        </form>

  </div>
 </div>
      </body>
</html>

index.jsp :

<form action="Myservlet" method="post"><br>
            User name`<`input type="text" name="username"`>`<br>
            Password `<`input type="password" name="password"><br>
            `<`input type="submit" value="Submit" `>`

</form>

Myservlet.java:

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

}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String user = request.getParameter("username");
    String pass = request.getParameter("password");
    RequestDispatcher rd;
    if (user.equals("username") && pass.equals("password")) {
        rd = request.getRequestDispatcher("/inscreption.jsp");
        rd.forward(request, response);
    } else {
        rd = request.getRequestDispatcher("/wrong.jsp");
        rd.forward(request, response);

    }
}

You can create more than two pages where you want to dispatch your request so here I created more than two jsp page names: right.jsp and wrong.jsp . If the username and password is correct then it frowards to right.jsp page if it's wrong, it frowards the request to wrong.jsp page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM