learn java
login.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ 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="loginAction1" >
<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>
loginAction1
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package vaannila;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ActionContext;
import java.util.*;
public class loginAction1 extends ActionSupport {
private String userId;
private String password;
@Override
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;
}
}
struts.xml
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<include file="example.xml"/>
<!-- Configuration for the default package. -->
<package name="default" extends="struts-default">
<action name="loginAction1" class="vaannila.loginAction1" >
<result name="success" type="dispatcher">/success2.jsp</result>
<result name="error" type="redirect">/login.jsp</result>
</action>
<action name="logoutAction" class="vaannila.logoutAction" >
<result name="success" type="redirect">checkLogin.jsp</result>
</action>
</package>
</struts>
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<include file="example.xml"/>
<!-- Configuration for the default package. -->
<package name="default" extends="struts-default">
<action name="loginAction1" class="vaannila.loginAction1" >
<result name="success" type="dispatcher">/success2.jsp</result>
<result name="error" type="redirect">/login.jsp</result>
</action>
<action name="logoutAction" class="vaannila.logoutAction" >
<result name="success" type="redirect">checkLogin.jsp</result>
</action>
</package>
</struts>
success2.jsp
<%@ taglib prefix="s" uri="/struts-tags" %>
<%@ page language="java" contentType="text/html" import="java.util.*"%>
<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() %>/vaannila/logoutAction">Logout</a>
<br />
</body>
</html>
<%@ page language="java" contentType="text/html" import="java.util.*"%>
<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() %>/vaannila/logoutAction">Logout</a>
<br />
</body>
</html>
logoutAction.java
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package vaannila;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ActionContext;
import java.util.*;
public class logoutAction extends ActionSupport {
@Override
public String execute() throws Exception {
Map session = ActionContext.getContext().getSession();
session.remove("logged-in");
return SUCCESS;
}
}
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package vaannila;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ActionContext;
import java.util.*;
public class logoutAction extends ActionSupport {
@Override
public String execute() throws Exception {
Map session = ActionContext.getContext().getSession();
session.remove("logged-in");
return SUCCESS;
}
}
after logoutAction it wil go to struts.xml and then to
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="login.jsp" />
</s:if>
</body>
</html>
output
<%@ 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="login.jsp" />
</s:if>
</body>
</html>
Thanks
ReplyDeletenice one ...
good!
ReplyDeletehi all please visit my codemyday.com for more struts2 tutorials.
ReplyDeleteexample is good but where is logout.jsp file
ReplyDelete