3

My project is to let an end user login and then go to another page to display a message. If the login details are wrong it displays an appropriate error message and gives them another chance to login in. The problem is that the message that is meant to be displayed is not. Instead all that is being displayed is;

Message is: ${message}

index.jsp

<form action="login.html" method="post">
    Name: <input type="text" name="name"/>
    Password: <input type="password" name="password"/>
    <input type="submit" value="submit"/>
</form>

LoginController.java

package com.loginmvc.domain;

@Controller
public class LoginController {

@RequestMapping("/login")
public ModelAndView login(HttpServletRequest req, HttpServletResponse resp) 
    throws ServletException, IOException {

    String name = req.getParameter("name");
    String password = req.getParameter("password");

    if(password.equals("admin")) {          
        String message = "Welcome " + name;
        return new ModelAndView("profilepage", "message", message);
    } else {
        return new ModelAndView("errorpage", "message", 
                "Sorry, the name or password is incorrect.");
    }
  }
}

web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
  <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>*.html</url-pattern>
  </servlet-mapping>
</web-app>

spring-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns:p="http://www.springframework.org/schema/p"  
    xmlns:context="http://www.springframework.org/schema/context"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
http://www.springframework.org/schema/context  
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="com.loginmvc.domain" />
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/jsp/" />
        <property name="suffix" value=".jsp" />
    </bean>
</beans>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
TheRealRave
  • 373
  • 1
  • 8
  • 21
  • The RealRave, can you solve your problem? I have same. And also it is not a proper solution for this case as claimed above. It is not duplicate. – aysegulP Nov 07 '19 at 14:22

2 Answers2

2

You are using an old descriptor in your web.xml file. This will result in the EL being ignored:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>

If you are following a tutorial, I would recommend that you find a newer one. However, in the meantime, updating your web.xml should solve your immediate problem:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
          http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">
...
</web-app>
StuPointerException
  • 7,117
  • 5
  • 29
  • 54
  • Hi, I tried this but unfortunately it still doesn't display the string. The funny is that I was able to display strings yesterday. – TheRealRave Jul 01 '16 at 13:55
0

Make sure that you are using : this Taglib in your JSP

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

and to print message : <c:if test="${not empty message}">${message}</c:if>

I highly recommend you to use IDE like eclipse or netbeans so that it would be easy to detect taglib errors and warning in your jsp.

nikesh adhikari
  • 113
  • 2
  • 11