File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes when i use login validator my validation not work properly Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "when i use login validator my validation not work properly" Watch "when i use login validator my validation not work properly" New topic

when i use login validator my validation not work properly

Aditya Bhardwaj

Joined: Aug 12, 2012
Posts: 4
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"

<display-name>Struts2 Application</display-name>


package net.aditya.struts2;

import java.util.Map;

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

public class AuthInterceptor implements Interceptor {

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


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


public String intercept(ActionInvocation invocation) throws Exception {
Map<String,Object> sessionAttribute=invocation.getInvocationContext().getSession();
return "failure";
return invocation.invoke();
return "failure";//go to interceptor failure



<?xml version="1.0" encoding="UTF-8" ?>
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

<constant name="struts.ui.theme" value="simple" />
<constant name="struts.custom.i18n.resources" value="ApplicationResources" />
<package name="anyUnigueName" extends="struts-default" >

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

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

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

<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 name="authorizedOnly" >
<interceptor-ref name="MyInterceptor"/>
<result name="success" >/Welcome.jsp</result>
<result name="failure">/Login.jsp</result>


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,getPassword());//method call
{ HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session=request.getSession();
session.setAttribute("authorized", "yes");
return "success";
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"));



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

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

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

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

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


Please Help
Aditya Bhardwaj

Joined: Aug 12, 2012
Posts: 4
Please help its really urgent
I agree. Here's the link:
subject: when i use login validator my validation not work properly
It's not a secret anymore!