'Passing value from java file to jsp file using struts
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.
Solution 1:[1]
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"/>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Karthik |
