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 ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

when i use login validator my validation not work properly

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"

<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
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
    Bookmark Topic Watch Topic
  • New Topic