Upload
digvijay-zala
View
284
Download
3
Embed Size (px)
Citation preview
Tell A FriendYour Friend Name
Java| Frameworks| Databases| Technology| Development| Build/Test tools| OS| Servers| PHP| Books| More| What's New?
Frameworks| Hibernate| Struts| JSF| JavaFX| Ajax| Spring| DOJO| JDO| iBatis| Questions? | Software Development
Search
iPhone Baby NamesApplication
Login/Logout With Session
In this section, we are going to develop a login/logout with session.
Login/Logout With Session
In this section, we are going
to develop a login/logout
application with session. This
application checks the user
authentication. Whenever you
run, it takes a user id and a
password (Both the user id
and password is "admin") it
displays the welcome page,
when both fields are correctly
filled.
Create an action mapping in the struts.xml file. Here is the code to be
added in the struts.xml:
<action name="login"class="net.roseindia.loginAction" > <result name="success"type="dispatcher">/pages/uiTags/Success.jsp</result> <result name="error"type="redirect">/pages/uiTags/Login.jsp</result></action>
<action name="logout"class="net.roseindia.logoutAction" > <result name="success"type="redirect">/pages/uiTags/checkLogin.jsp</result></action>
Develop an action class that handles the login request. The Struts 2
framework provides a base ActionSupport class that implements
commonly used framework interfaces. In our action class
(loginAction.java) we have extended ActionSupport class.
ProgrammingTutorialsSpringFrameworkHibernate-TutorialsAjaxCore JavaDatabase-TutorialsEnterpriseJavaiPhoneJ2ME-TutorialsJava-TutorialsJavaScriptJDBC-TutorialsJMS-TutorialsJPAJSP-TutorialsOpen SourcePHPServlets-TutorialsStruts-TutorialsTechnologyUML-TutorialsWeb-Services-TutorialsXMLMoreTutorials....
Ads by Google Struts Class Action Support for Struts Struts 2 Tutorial
application
Struts 2 Login,Struts 2 Session http://www.roseindia.net/struts/struts2/struts2-login....
1 of 8 15-10-2010 07:24
Here is the code of "loginAction" action class:
package net.roseindia;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ActionContext;
import java.util.*;
public class loginAction extends ActionSupport {
private String userId;
private String password;
public String execute() throws Exception{
if ("admin".equals(userId) && "admin".equals(password)) {
Map session = ActionContext.getContext().getSession();
session.put("logged-in","true");
return SUCCESS;
}
else{
return ERROR;
}
}
public String logout() throws Exception {
Map session = ActionContext.getContext().getSession();
session.remove("logged-in");
return SUCCESS;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
}
Download this code.
Again, develop an action class to handle the logout operation. An action
class (logoutAction) we have extended ActionSupport class.
Here is the code of logoutAction action class:
package net.roseindia;
import javax.servlet. .HttpSession;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ActionContext;
import java.util.*;
public class logoutAction extends ActionSupport {
public String execute() throws Exception {
Map session = ActionContext.getContext().getSession();
session.remove("logged-in");
return SUCCESS;
}
}
Download this code.
Develop Login Form: The GUI of the application consists of login
form (Login.jsp). The "Login.jsp" displays the login page to the
user.
Here is the code of Login.jsp file:
<%@ taglib prefix="s" uri="/struts-tags" %>
Recently Viewed
JSP Alert
JSP CheckBox
JSP decode URL
Duplicated Session Variables
JSP Frameset
JSP get Parameter
JSP Get URL
JSP Include File
JSP Include jsp
JSP Include Param
Software Solutions
Software Solutions and Services
Website Designing Services
Web Designing Packages! From $150!
Flex Outsourcing
Hire Flex Developer
Website Designing Company Web Hosting
Website Designing Quotation
Hire PHP Developer
Hire PHP programmer
PHP Outsourcing
Java Project Outsourcing
Hire Software Developer
Hire Java Developer
Hire iPhone developer
Outsourcing iPhone development
Offshore iPhone Apps Development
Search Tutorials
Ads by Google
Java Class
J2EE Class
Code Class
Logic Class
http
Struts 2 Login,Struts 2 Session http://www.roseindia.net/struts/struts2/struts2-login....
2 of 8 15-10-2010 07:24
<%@ page language="java" contentType="text/html"%>
<html>
<head>
<title>Insert Data here!</title>
<link href="<s:url value="/css/main.css"/>" rel="stylesheet"
type="text/css"/>
</head>
<body>
<s:form action="/roseindia/login.action" method="POST">
<s:textfield name="userId" label="Login Id"/><br>
<s:password name="password" label="Password"/><br>
<s:submit value="Login" align="center"/>
</s:form>
</body>
</html>
The "Success.jsp" page displays the Login Success message
(Welcome, you have logged-in.) and session (Session Time: Wed Aug
01 11:26:38 GMT+05:30 2007 and Logout ) when user gets successful
authentication. If you click the "Logout" then again login page is
displayed on the screen.
Here is the code of Success.jsp file:
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page language=" " contentType="text/html" import="java.util.*"%>
<jsp:include page="/struts2tags/pages/uiTags/loginCheck.jsp" />
<html>
<head>
<title>Welcome, you have logined!</title>
</head>
<body>
Welcome, you have logined. <br />
<b>Session Time: </b><%=new Date(session.getLastAccessedTime())%>
<br /><br />
<a href="<%= request.getContextPath() %>/roseindia/logout.action">Logout</a>
<br />
</body>
</html>
This page logs out the valid user.
checkLogin.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page language="java" contentType="text/html" import="java.util.*"%>
<html>
<head>
<title>Check validate!</title>
</head>
<body>
<s:if test="#session.login != 'admin'">
<jsp:forward page="/pages/uiTags/Login.jsp" />
</s:if>
</body>
</html>
Output:
Run this application by getting the login page:
Enter the wrong user id and password in the login page
java
Struts 2 Login,Struts 2 Session http://www.roseindia.net/struts/struts2/struts2-login....
3 of 8 15-10-2010 07:24
You get the following output:
Enter the correct user id and password in the login page:
You get the following output:
After clicking the "Logout". You get the following output:
Struts 2 Login,Struts 2 Session http://www.roseindia.net/struts/struts2/struts2-login....
4 of 8 15-10-2010 07:24
Related Links by Google
Struts Tutorials - Jakarta Strut Tutorials,Struts,Struts Exam...
INTRODUCION TO STRUTS FRAMEWORK
Struts Guide
Struts 2 Tutorial,Struts2 Examples,Apache Struts 2 Tutorials ...
Struts Projects,Struts Project
Struts Articles
Related Searches by Google
struts
shocks and struts
struts 2
login
» View all relatedtutorials
Validations using Struts 2 Annotations ... 2709 views
Struts 2 Downloads ... 618944 views
Struts 2 Tags Examples ... 619101 views
Struts 2 Url Validator ... 619145 views
Struts 2 double validator ... 619562 views
Struts 2 Tags (UI Tags) Examples ... 619663 views
Struts 2.0.4 Released ... 619753 views
Validations using Struts 2 Annotations ... 619768 views
Struts 2.0.2 Released ... 619841 views
Struts 2.0.3 Released ... 619859 views
» View all related tutorials
12
345
Related Tags: java c list framework template struts io tags downloadannotations load sed ado release javadoc new tag aggregate changefeatures
Leave your comment:
Ads by Google JSP Class JSP Session BMW X5 Struts Coding Class
More in this series... Most Read Latest Search
Struts 2 Login,Struts 2 Session http://www.roseindia.net/struts/struts2/struts2-login....
5 of 8 15-10-2010 07:24
Name:
Email:
URL: http://www.roseindia.net/struts/struts2/struts2
Title:
Comments:
Enter
Code:
Submit Comment
Note: Emails will not be visible or
used in any way, and are not
required. Please keep comments
relevant. Any content deemed
inappropriate or offensive may
be edited and/or deleted.
No HTML code is allowed. Line
breaks will be converted
automatically. URLs will be
auto-linked. Please use BBCode
to format your text.
Add This Tutorial To:
Del.icio.us
Digg
Spurl
Blink
Furl
Simpy
Y!
MyWeb
Current Comments
10 comments so far (post your own) View All Comments Latest 10Comments:
Where does session.login came from?
Posted by SunriseKid on Monday, 09.15.08 @ 08:33am | #79487
View This Comment Separately
hi to everybody,
in the article above the user needs to login and logout explicitally.
i need that the logout occours also when the user closes its browser and
when the session expires.
someone can help me?
thank's
Posted by michele on Saturday, 08.2.08 @ 15:30pm | #70529
View This Comment Separately
I'm a beginer of Struts Framework Programing.
i just started with this Login tiutorial but i got error at Runtime like this.
"The Struts dispatcher cannot be found. This is usually caused by using
Struts tags without the associated filter. Struts tags are only usable
when the request has passed through its servlet filter, which initializes
Struts 2 Login,Struts 2 Session http://www.roseindia.net/struts/struts2/struts2-login....
6 of 8 15-10-2010 07:24
the Struts dispatcher needed for this tag. "
at org.apache.struts2.views.jsp.TagUtils.getStack(TagUtils.java:60)
which is the
inbuit class of strtus.jar file .
i'm using Oracle's Jdeveloper for .
plz give me solution for this problem as soon as possible.
Thank you,
Rose India
Posted by Mayank Patel on Monday, 05.12.08 @ 14:24pm | #59449
View This Comment Separately
<s:if test="#session.login != 'admin'">
where does login come from?
Posted by struts beginner on Saturday, 05.10.08 @ 02:53am |
#59055
View This Comment Separately
Thanks for giving these tutorials which will help all the beginners in
struts.
I have some doubt in this tutorial(Login/Logout With Session). I am
listing it below.
1) What's the need of
public String logout() throws Exception {
Map session = ActionContext.getContext().getSession();
session.remove("logged-in");
return SUCCESS;
}
in loginAction.java
2)what's the need of
<jsp:include page="/struts2tags/pages/uiTags/loginCheck.jsp" />
in Success.jsp
I got the results without these. So what for it is written?
Hope i will get help form yours.
Thanks in Advance
Struts
Posted by sarin on Tuesday, 03.18.08 @ 10:20am | #53142
View This Comment Separately
Highly informative for struts2 beginners.
many thanks
Posted by dtrprasad on Wednesday, 03.12.08 @ 20:26pm | #52492
file
development
Struts 2 Login,Struts 2 Session http://www.roseindia.net/struts/struts2/struts2-login....
7 of 8 15-10-2010 07:24
View This Comment Separately
Hi, I'm currently learning the Struts2 through this tutorial, but at this
page, I got some error. When I do exactly what this page said, I can't
access the login.action, it says that there's some null pointer exception;
then I delete the "type=redirect" in the struts. , in the login
action, it works. I don't know the reason:(
and my jdk is 1.5.0.13, tomcat version 6.0.16
can any one explain this? Thanks:)
Posted by stanley on Wednesday, 02.27.08 @ 08:30am | #50264
View This Comment Separately
my question is the same of vijaykumar
Posted by Cynthiadave on Sunday, 12.16.07 @ 06:01am | #42566
View This Comment Separately
The Success.jsp has this include - but I don't see where it is, and the
struts2tutorial.zip is missing all kinds of files from here.
Posted by Sean on Saturday, 12.8.07 @ 02:25am | #41570
View This Comment Separately
Hi
it is very usefull site for begginers like me. i have gone through this
sessions. i am having query related to session.put("logged-
in","true").my query here is where do we use those objects which are
declared in
session.put().
i would like to know about this.can any one help me here...........
Posted by vijaykumar on Tuesday, 11.27.07 @ 13:02pm | #40676
View This Comment Separately
Home | JSP | EJB | JDBC | Java Servlets | WAP | Free JSP Hosting | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs
About Us | Advertising On RoseIndia.net | Site Map | Privacy Policy
Send your comments, Suggestions or Queries regarding this site at [email protected].
Copyright © 2008. All rights reserved.
404
xml file
Struts 2 Login,Struts 2 Session http://www.roseindia.net/struts/struts2/struts2-login....
8 of 8 15-10-2010 07:24