1. Create one MyFirstStruts Web Dynamic Project (Say C:\workspaceAll\Struts\FirstStruts)
2. Create JSP pages
a) CustomerForm.jsp (C:\workspaceAll\Struts\MyFirstStruts\WebContent)
b) Success.jsp (C:\workspaceAll\Struts\MyFirstStruts\WebContent\Success.jsp)
3. Configuration Files
a) web.xml (C:\workspaceAll\Struts\MyFirstStruts\WebContent\WEB-INF\web.xml)
b) struts-config.xml (C:\workspaceAll\Struts\MyFirstStruts\WebContent\WEB-INF\struts-config.xml)
4. Java Files
a) CustomerForm.java (C:\workspaceAll\Struts\MyFirstStruts\src\CustomerForm.java)
b) CustomerAction.java (C:\workspaceAll\Struts\MyFirstStruts\src\CustomerAction.java)
5. Put these jar files (C:\workspaceAll\Struts\MyFirstStruts\WebContent\WEB-INF\lib)
a) commons-beanutils.jar
b) commons-collections.jar
c) commons-digester.jar
d) commons-logging.jar
e) struts.jar
6. Put struts-html.tld into C:\workspaceAll\Struts\MyFirstStruts\WebContent\WEB-INF\struts-html.tld
CustomerForm.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<html:html xhtml="true">
<body>
<html:form action="/submitCustomerForm">
Put Customer First Name <html:text property="firstName" size="16" maxlength="16"/> <BR/>
Put Customer Last Name <html:text property="lastName" size="16" maxlength="16"/> <BR/> <P/>
<html:submit>Save</html:submit>
</html:form>
</body>
</html:html>
Success.jsp
<html>
<h1> THIS IS SUCCESS PAGE </h1>
</html>
CustomerForm.java
import org.apache.struts.action.ActionForm;
public class CustomerForm extends ActionForm {
private String firstName;
private String lastName;
public CustomerForm() { firstName = ""; lastName = ""; }
public String getFirstName() { return firstName; }
public void setFirstName(String s) { this.firstName = s; }
public String getLastName() { return lastName; }
public void setLastName(String s) { this.lastName = s; }
}
CustomerAction.java
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 CustomerAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { ActionForward nextPage = null;
CustomerForm custForm = (CustomerForm) form;
String firstName = custForm.getFirstName();
String lastName = custForm.getLastName();
System.out.println("Customer First name is " + firstName);
System.out.println("Customer Last name is " + lastName);
nextPage = mapping.findForward("success");
return nextPage; }
}
web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<!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> <load-on-startup>1</load-on-startup> </servlet>
<servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <taglib> <taglib-uri>/WEB-INF/struts-html.tld</taglib-uri> <taglib-location>/WEB-INF/struts-html.tld</taglib-location> </taglib> </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> <form-beans> <form-bean name="myForm" type="CustomerForm" /> </form-beans>
<action-mappings> <action path="/submitCustomerForm" type="CustomerAction" name="myForm" scope="request"> <forward name="success" path="/Success.jsp" /> </action> </action-mappings>
</struts-config>
Start the server:
http://localhost:8080/MyFirstStruts/CustomerForm.jsp
Put First Name: Binod
Put Second Name: Suman
Click on Save button and check the server console, you should be get
Customer First name is Binod
Customer Last name is Suman
and new success page will come with message:
THIS IS SUCCESS PAGE
That's it.
Wednesday, February 25, 2009
Tuesday, February 24, 2009
Spring MVC simple example : Spring Tutorial
This is very basic running example for spring MVC. I developed this project in IMB RAD (
Rational Software Development Platform 6.0) and WebSphere Application Server v6.0.
Step1: Create one Dynamic web project (File -> New -> Dynamic Web Project) and give some name (Say SpringTrans).
Step2: Add spring.jar to project and copy spring.jar to WEB-INF\lib folder
Step3: Edit web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd%22>
<display-name>SpringTrans</display-name>
<servlet>
<servlet-name>SpringTrans</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringTrans</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>
Step 4: Create one xml file SpringTrans-servlet.xml
Note: Name must be SpringTrans-servlet.xml, since we have given the servlet name SpringTrans, so it is SpringTrans-servlet.xml
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd%22>
<beans>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="urlMap">
<map>
<entry key="/loginCheck.htm" value-ref="userController"/>
</map>
</property>
</bean>
<bean id="userController" class="com.controller.UserController">
<property name="methodNameResolver">
<bean class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
<property name="mappings">
<props>
<prop key="/loginCheck.htm">loginHandler</prop>
</props>
</property>
</bean>
</property>
</bean>
</beans>
Step 5: In Java Resource, create one package com.controller
Step 6: Create one Java file in this package UserController.java
package com.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
public class UserController extends MultiActionController {
public ModelAndView loginHandler( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("*********** FROM CONTROLLER *************");
String userInfoName = request.getParameter("userName");
String pass = request.getParameter("password");
System.out.println("User Name :: "+userInfoName);
System.out.println("Password :: "+pass);
return new ModelAndView("/SucessPage.jsp","user",userInfoName);
}
}
Step 7: Create jsp file inside WebContent folder
login.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Login Page</TITLE>
</HEAD>
<BODY>
<P>LOGIN PAGE<BR></P>
<form action="loginCheck.htm">
<TABLE>
<TBODY>
<TR>
<TD width="326" align="right">User Name</TD>
<TD width="311"><input type="text" name="userName"/></TD>
</TR>
<TR>
<TD width="326" align="right">Password</TD>
<TD width="311"><input type="text" name="password"/></TD>
</TR>
<TR>
<TD width="326" align="right"><input type="submit" value="OK"/></TD>
<TD width="311"><input type="button" value="Cancel"/></TD>
</TR>
</TBODY>
</TABLE>
</form>
</BODY>
</HTML>
Step 8: SucessPage.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<TITLE>SucessPage Page</TITLE>
</HEAD>
<BODY>
<P>Your Login is Sucessfull</P>
<%
String userName = request.getParameter("userName");
try{ out.print(userName); }catch(Exception e){}
%>
</BODY>
</HTML>
Step9:
Run http://localhost:9080/SpringTrans/
Rational Software Development Platform 6.0) and WebSphere Application Server v6.0.
Step1: Create one Dynamic web project (File -> New -> Dynamic Web Project) and give some name (Say SpringTrans).
Step2: Add spring.jar to project and copy spring.jar to WEB-INF\lib folder
Step3: Edit web.xml
<?xml version="1.0" encoding="UTF-8"?><web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd%22>
<display-name>SpringTrans</display-name>
<servlet>
<servlet-name>SpringTrans</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringTrans</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>
Step 4: Create one xml file SpringTrans-servlet.xml
Note: Name must be SpringTrans-servlet.xml, since we have given the servlet name SpringTrans, so it is SpringTrans-servlet.xml
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd%22>
<beans>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="urlMap">
<map>
<entry key="/loginCheck.htm" value-ref="userController"/>
</map>
</property>
</bean>
<bean id="userController" class="com.controller.UserController">
<property name="methodNameResolver">
<bean class="org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
<property name="mappings">
<props>
<prop key="/loginCheck.htm">loginHandler</prop>
</props>
</property>
</bean>
</property>
</bean>
</beans>
Step 5: In Java Resource, create one package com.controller
Step 6: Create one Java file in this package UserController.java
package com.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
public class UserController extends MultiActionController {
public ModelAndView loginHandler( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("*********** FROM CONTROLLER *************");
String userInfoName = request.getParameter("userName");
String pass = request.getParameter("password");
System.out.println("User Name :: "+userInfoName);
System.out.println("Password :: "+pass);
return new ModelAndView("/SucessPage.jsp","user",userInfoName);
}
}
Step 7: Create jsp file inside WebContent folder
login.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Login Page</TITLE>
</HEAD>
<BODY>
<P>LOGIN PAGE<BR></P>
<form action="loginCheck.htm">
<TABLE>
<TBODY>
<TR>
<TD width="326" align="right">User Name</TD>
<TD width="311"><input type="text" name="userName"/></TD>
</TR>
<TR>
<TD width="326" align="right">Password</TD>
<TD width="311"><input type="text" name="password"/></TD>
</TR>
<TR>
<TD width="326" align="right"><input type="submit" value="OK"/></TD>
<TD width="311"><input type="button" value="Cancel"/></TD>
</TR>
</TBODY>
</TABLE>
</form>
</BODY>
</HTML>
Step 8: SucessPage.jsp
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<TITLE>SucessPage Page</TITLE>
</HEAD>
<BODY>
<P>Your Login is Sucessfull</P>
<%
String userName = request.getParameter("userName");
try{ out.print(userName); }catch(Exception e){}
%>
</BODY>
</HTML>
Step9:
Run http://localhost:9080/SpringTrans/
Wednesday, February 11, 2009
How to show Image on JSP page from My Computer Folder
Please visit my another blog http://binodservlet.blogspot.com/
Subscribe to:
Posts (Atom)