0

I am trying to implement a basic struts application where I am trying to pass a string value from a java file to jsp. But I am getting a null value in the jsp page. I have been working on this for 2-3 days yet I have not been able to figure out the issue. Please help me out

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>

    <display-name>Hello World Struts Application</display-name>

    <servlet>
        <servlet-name>action</servlet-name>
        <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
        <init-param>
            <param-name>config</param-name>
            <param-value>/WEB-INF/struts-config.xml</param-value>
        </init-param>
        <init-param>
            <param-name>debug</param-name>
            <param-value>3</param-value>
        </init-param>
        <init-param>
             <param-name>detail</param-name>
             <param-value>3</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>action</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>


    <welcome-file-list>
        <welcome-file>view.jsp</welcome-file>
    </welcome-file-list>

</web-app>

struts-config.xml

<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
    <action-mappings>
        <action path="/view" type="myAction" validate="false">
                <forward name="success" path="/first" />
        </action>
        <action path="/view"
                forward="/view.jsp"/>
        <action path="/first" type="myAction" validate="false">
            <forward name="success" path="/first.jsp" />
        </action>
    </action-mappings>
</struts-config>

view.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>

    <body>
        <form action="first.do">
            Enter name :
            <input type="text" name="name"/>
            <input type="submit" value="Enter"/>
        </form>

    </body>
</html>

first.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>

    <body>
        Welcome!!!!!!!!

        <%
            String s=(String)request.getAttribute("s");
            out.println("s="+s);
        %>
    </body>
</html>

myAction.java

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class myAction extends Action {
    public ActionForward execute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException {

        String s="Karthikeyan";

        request.setAttribute("s",s);

        RequestDispatcher reqDispatcher = getRequestDispatcher("view.jsp");
        reqDispatcher.forward(request,response);




        return (mapping.findForward("success"));
    }
}

How to check whether value is getting passed from the java file to the jsp? Thank you in advance for the help.

4
  • If you want it to persist across requests use the session. Apr 15, 2014 at 7:04
  • You don't have to use reqDispatcher anywhere in action as you are using struts. It is strut's responsibility to forward your request on specified jsp Apr 15, 2014 at 7:06
  • @user3260147 In the above code, which would succeed, the reqDispatcher.forward or the mapping.findForward ? Apr 15, 2014 at 7:09
  • if you can paste stcaktrace here
    – jmail
    Apr 15, 2014 at 7:26

1 Answer 1

0

declare string outside execute method and keep its getter setters. Then initialize it to your value in execute method. now use property tag in jsp to get its value

public class Test extends ActionSupport
{
    String s;

    public String getS() {
        return s;
    }

    public void setS(String s) {
        this.s = s;
    }

    public String execute()
    {
        s="MyName";
        return SUCCESS;
    }

}

and my jsp page contains<s:property value="s"/>

1
  • can you pleaes provide some source code to help the asker understand what you mean here?
    – L0j1k
    Apr 15, 2014 at 9:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.