java - Sending values from jsp to servlet then back to jsp -


i know similar questions have been asked earlier reason not working. trying check if user has entered both fields on login page or not. if not, want display message on jsp saying need enter both credentials. here jsp:

<%@ page language="java" contenttype="text/html; charset=iso-8859-1" pageencoding="iso-8859-1"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>  <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1"> <title>login</title> </head> <body>     <h2>some app</h2>     <form action="login" method="post">         <table>             <tr>                 <td>username</td>                 <td><input type="text" name="uname"></td>             </tr>             <tr>                 <td>password</td>                 <td><input type="password" id="pass" name="pass"></td>             </tr>         </table>         <br> <input type="button" value="submit">     </form>     <c:out value= "${error}"/> </body> </html> 

then here servlet:

@webservlet("/login") public class login extends httpservlet {     private static final long serialversionuid = 1l;      /**      * @see httpservlet#httpservlet()      */     public login() {         super();      }      /**      * @see httpservlet#dopost(httpservletrequest request, httpservletresponse response)      */     protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {          httpsession session = request.getsession();          //getting values form         string username = request.getparameter("uname");         string password = request.getparameter("pass");          system.out.println("username is: "+ username);         system.out.println("password is: "+ password);          //user user = new user();          if((username.equals(""))|| password.equals("")){              string message = "please enter both credentials";             request.setattribute("error", message);             //requestdispatcher rd = request.getrequestdispatcher("/login.jsp");             //rd.forward(request, response);             getservletcontext().getrequestdispatcher("/login.jsp").forward(request, response);          }         else{             requestdispatcher rd = request.getrequestdispatcher("/index.jsp");             rd.forward(request, response);         }          //setting values in session         session.setattribute("username", username);         session.setattribute("password", password);     } } 

since trying experiment maven, here pom.xml file dependency added jstl jar:

<dependency>     <groupid>javax.servlet</groupid>     <artifactid>jstl</artifactid>     <version>1.2</version> </dependency> 

why c:out tag not print anything? doing wrong? appreciated. thank you.

you mixing 2 styles of returning response client. try passing error message using code:

if((username == null)|| password == null) {                   string message = "please enter both credentials";     request.setattribute("error", message);     getservletcontext().getrequestdispatcher("/login.jsp").forward(request, response); } 

the rule of thumb have use request.setattribute() forward() , request.getsession().setattribute() response.sendredirect().


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -