This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Svelte and Sapper in Action and have Mark Volkmann on-line!
See this thread for details.
Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

when i use login validator my validation not work properly

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am new to struts 2.0 interceptor when user login and successfully validate he goes to a new Page but when he copies the url and enter in some new browser for directly accessing the welcome page it must redirect to login page but it give null pointer exception please help it really urgent thanks in advance

My web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" 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">;

<display-name>Struts2 Application</display-name>
<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>

AuthInterceptor.java

package net.aditya.struts2;

import java.util.Map;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class AuthInterceptor implements Interceptor {

@Override
public void destroy() {
// TODO Auto-generated method stub

}

@Override
public void init() {
// TODO Auto-generated method stub

}

@Override
public String intercept(ActionInvocation invocation) throws Exception {
Map<String,Object> sessionAttribute=invocation.getInvocationContext().getSession();
if(sessionAttribute==null||sessionAttribute.get("authorized")==null)
{
return "failure";
}
else{
if(((String)sessionAttribute.get("authorized")).equals("yes"))
{
return invocation.invoke();
}
else
return "failure";//go to interceptor failure
}

}
}

struts.xml

<?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>
<constant name="struts.ui.theme" value="simple" />
<constant name="struts.custom.i18n.resources" value="ApplicationResources" />
<package name="anyUnigueName" extends="struts-default" >

<interceptors>
<interceptor name="authInterceptor" class="net.aditya.struts2.AuthInterceptor">

</interceptor>
<interceptor-stack name="MyInterceptor"> <!-- so that all interceptor like validation work properly -->
<interceptor-ref name="defaultStack" />
<interceptor-ref name="authInterceptor" /> <!-- add custom interceptor -->
</interceptor-stack>
</interceptors>



<action name="login">
<result >/Login.jsp</result>
</action>

<action name="authorized" class="net.aditya.struts2.LoginAction" method="loginCredentialCheck"><!-- if you not provide method name then by default execute is call -->
<result name="success" type="chain">authorizedOnly</result>
<result name="failure">/Login.jsp</result>
<result name="input">/Login.jsp</result>
</action>

<action name="authorizedOnly" >
<interceptor-ref name="MyInterceptor"/>
<result name="success" >/Welcome.jsp</result>
<result name="failure">/Login.jsp</result>
</action>

</package>
</struts>


LoginAction.java

package net.aditya.struts2;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import businessService.Login;

import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport{//Action Support is for addActionError msg



private String username;
private String password;



public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}


public String loginCredentialCheck() {

Login obj=new Login();
String b=obj.compare(getUsername(),getPassword());//method call
if(b.equals("match"))
{ HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session=request.getSession();
session.setAttribute("authorized", "yes");
return "success";
}
else
{
addActionError("Please provide right Credentials");// you can directly put ant string msg here
return "failure";
}

}


public void validate() {
if (getUsername().length() == 0) {

addFieldError("username", "User Name is required");
}

else if (getPassword().length() == 0) {

addFieldError("password", getText("password.required"));
}
}

}

Login.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">;
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<s:actionerror/>
<s:form action="authorized" method="post" >

<table align="center" bgcolor="red" width="80%">
<tr>
<th align="center">Login Form</th>
</tr>

<tr>
<td>Username</td>
<td><s:textfield name="username" /></td>
<td><s:fielderror fieldName="username" /> </td>
</tr>

<tr>
<td>Password</td>
<td><s:password name="password" /></td>
<td><s:fielderror fieldName="password" /> </td>
</tr>

<tr>
<td><s:submit label="Submit" align="center"/></td>
<td><a href="registration.jsp">create Account</a></td>
</tr>



</table>
</s:form>
</body>
</html>

Please Help
 
Aditya Bhardwaj
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please help its really urgent
 
Beauty is in the eye of the tiny ad.
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic