Jsp , struts 2 example in eclipse (Struts login application )

JSP (JavaServer Page) :
JavaServer Page allows programmer to embed java code directly in the HTML code.So we can easily …embed java code in web pages.
Jsp can call custom java classes (taglibs) with HTML-like tags.
Separates both dynamic content(java code) and presentation(HTML code) of a Web page.
Struts :
Apache struts is an open source Java EE framework to develop web applications.It supports model-view-controller (MVC) architecture.It is a widely used framework for application development.
It is not an installable software , so add the struts related jar files to the lib folder.
Basic requirements for Jsp , struts 2 example .
Softwares : |
Java | (Download). |
Oracle Database | (Download). |
GlassFish server | (Download). |
Eclipse IDE | (Download). |
Jar files: (Download). |
commons-fileupload-1.3.1.jar | |
commons-io-2.2.jar | |
commons-lang3-3.1.jar | |
commons-logging-1.1.3.jar | |
commons-logging-api-1.1.jar | |
freemarker-2.3.19.jar | |
javassist-3.11.0.GA.jar | |
ognl-3.0.6.jar | |
struts2-core-2.3.16.3.jar | |
xwork-core-2.3.16.3.jar |
Source files : |
login.jsp | |
LoginActionClass.java | |
struts.xml | |
web.xml | |
success.jsp | |
error.jsp |
Jsp , struts 2 example code :
Directory Structure in eclipse :
Execution Flow :
1 . Server accepts the request from the client window and it verifies the web.xml <url-pattern> from the web application.
2. Next it will check the <filter-name> inside the <filter-mapping>.
3. Later checks for the matching <filter-name> in the web.xml.
4. If the identical name exists , it will call the StrutsPrepareAndExecuteFilter .
5. Next it will check the action name (login) in the struts.xml .
6. After that ,it loads the class and execute the execute() method.
7. Based on the execution , it loads the appropriate response page(Exampel :success.jsp).
8. Finally the response page goes to the client window.
login.jsp
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="h" uri="/struts-tags" %> <html> <body> <h:form action="login" method="post"> <h:textfield name="userName" label="Enter User Name" /><br> <h:password name="userPassword" label="Enter Password" /><br> <h:submit value="Login" align="center" /> </h:form> </body> </html> |
web.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>login.jsp</welcome-file> </welcome-file-list> </web-app> |
LoginActionClass.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
package strutsApp; import com.opensymphony.xwork2.ActionSupport; public class LoginActionClass extends ActionSupport { private static final long serialVersionUID = 1L; private String userName,userPassword; public String execute() { if(userName.equals("java2db") && userPassword.equals("password")) { System.out.println("verification is success"); return "success"; }else return "error"; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserPassword() { return userPassword; } public void setUserPassword(String userPassword) { this.userPassword = userPassword; } } |
struts.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <include file="struts-default.xml"/> <package name="strutsApp" extends="struts-default"> <action name="login" class="strutsApp.LoginActionClass"> <interceptor-ref name="timer"/> <interceptor-ref name="logger"/> <interceptor-ref name="completeStack"/> <result name="success">/success.jsp</result> <result name="error">/error.jsp</result> </action> </package> </struts> |
success.jsp
1 2 |
<%@ taglib prefix="h" uri="/struts-tags" %> Hello <h:property value="userName" />, you have been successfully logged in |
error.jsp
1 2 |
<%@ taglib prefix="s" uri="/struts-tags" %> Invalid User Name/Password.... |
Execution result :
Enter the User name and password in the login screen and click Login button.

Related Posts : |
![]() |
RESTful web service example in java using JAX-RS |
![]() |
Call database in Jsp (Access Oracle , MySQL , PostgerSQL ,Db2 from Jsp) |
![]() |
How to integrate Struts 2 and Hibernate 5 (Login example) |
![]() |
How to integrate Jsp , Struts 2 and hibernate 4 ( Login example ) |
![]() |
Jsp Servlet and Hibernate 4 login example in Eclipse |
![]() |
Jsp , Servlet and JDBC login application example in NetBeans |
![]() |
Jsp,Servlet and JDBC integration example in Eclipse |
![]() |
Jsp-Servlet example in Eclipse,NetBeans and MyEclipse |
Please post Spring…..