STRUTS BASIC EXAMPLE
This is the first Example in developing a web application using STRUTS framework.
Objective: Struts basic example .
User Enters enters name as input and the same is to be produced as Output .
Prerequisites : Java 1.6 , Apache Server 6.0 ,Struts Framework
Things you need to aware of : ACTION class
Application Structure :
Project Name : BasicStruts
public class MyAction extends Action
{
This is the first Example in developing a web application using STRUTS framework.
Objective: Struts basic example .
User Enters enters name as input and the same is to be produced as Output .
Prerequisites : Java 1.6 , Apache Server 6.0 ,Struts Framework
Things you need to aware of : ACTION class
Application Structure :
index.jsp
----------------
// This is the welcome page loaded on browser when the application is executed
<form action="/BasicStruts/login.do">
<input type="text" name="name">
<input type="submit" name="submit" value="submit">
</form>
<input type="text" name="name">
<input type="submit" name="submit" value="submit">
</form>
Here When index.jsp is submitted to server then request is handled by specific action class which is referred by " /BasicStruts/login.do ".
So the path /login.do is referred in struts-config.xml .
WelcomeStruts.jsp
------------------------------
// This jsp page presents the output to the user as specified by action class.
<%String username= (String) session.getAttribute("username"); %>
Hello <%= username%>..... Welcome to Struts ..
web.xml
-----------------
<web-app>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet </servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet </servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
web.xml is configured such that every request will come to same servlet org.apache.struts.action.ActionServlet .
ActionServlet serves as FrontController .
ActionServlet routes all the requests to the RequestProcessors
struts-config.xml
--------------------------
<struts-config>
<action-mappings>
<action path="/login" type="org.kmr.struts.MyAction ">
< forward name="showname" path="/WelcomeStruts.jsp"/>
</action>
</action-mappings>
</struts-config>
<action-mappings>
<action path="/login" type="org.kmr.struts.MyAction ">
< forward name="showname" path="/WelcomeStruts.jsp"/>
</action>
</action-mappings>
</struts-config>
/login------> It is the request URL as specified in the jsp file index.jsp
org.kmr.struts.MyAction ----> It is the Action Class which handles any reuest of the form /login.do
Action Class ( MyAction.java )
--------------------------------------------------
package org.kmr.struts;
import org.apache .struts.action.*;
import javax.servlet.http.*;
import javax.servlet.http.*;
public class MyAction extends Action
{
public ActionForward execute( ActionMapping am, ActionForm af, HttpServletRequest req, HttpServletResponse res) throws Exception
{
String name=req.getParameter("name");
HttpSession session= req.getSession();
session.setAttribute("username",name);
return am.findForward("showname");
}
{
String name=req.getParameter("name");
HttpSession session= req.getSession();
session.setAttribute("username",name);
return am.findForward("showname");
}
}
When the line return am.findForward("showname"); is encounterd then struts-config.xml is referred
at the line < forward name="showname" path="/WelcomeStruts.jsp"/> , so WelcomeStruts.jsp is sent as a response to the browser .