Struts - Redirect to start by accessing jsp-file?

Hello,

Right to the point:

Is there a way to redirect an user right back to the start ( login-page) if he tries to directly access a jsp-site?

I prevented it by moving all jsp-files into a subfolder of WEB-INF.

Its not pretty but it works.... But id rather use another aproach.. - including the mentioned redirection.

A code example would be lovely

thanks for the help!

[413 byte] By [nomaxa] at [2007-11-27 10:49:25]
# 1

Use something like this:

public class CustomRequestProcessor extends RequestProcessor {

/**

* @see org.apache.struts.action.RequestProcessor#processPreprocess(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)

*/

protected boolean processPreprocess(HttpServletRequest request, HttpServletResponse response) {

boolean continueProcessing = true; //Assume that everything is cool

try{

HttpSession session = null;

//check if the session has expired

if(request.isRequestedSessionIdValid())

session = request.getSession();

else //session expired

response.sendRedirect("login.jsp");

//Get the o request path

String path = processPath(request, response);

//Don't do anything if the user is logging in

if(!path.equals((String)"/login")){

UserVO user = (UserVO)session.getAttribute("user");

if(user== null){

response.sendRedirect("login.jsp");

continueProcessing = false;

}

}

}catch(Exception e){

continueProcessing = false;

}

return continueProcessing;

}

This acts as a filter.

Manuel Leiria

manuel.leiriaa at 2007-7-29 11:19:06 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

And in your struts-config.xml

<!-- ======================================== Controller Definition -->

<controller processorClass="yourpackage.CustomRequestProcessor"/>

Manuel Leiria

manuel.leiriaa at 2007-7-29 11:19:06 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

your class makes sense,

I guess I have to itegrate it somehow in my struts-config.xml... so it will be checked everytime a access takes place.

Can u tell me where to connect it with the workflow?

Ive updated my struts-config.xml. and I created a new class named CustomRequestProcessor.

But its full of errors... i guess lots and lots of imports are missing. do u have the complete file?

thanks!

Message was edited by:

nomax

nomaxa at 2007-7-29 11:19:06 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

If you just copy pasted, of course it has errors.

The imports:

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

import org.apache.struts.action.RequestProcessor;

Now,

UserVO is an object created by my to store user info (username, roles...)

Check the paths (/somepath/.../login.jsp)

In struts-config.xml check the package

Manuel Leiria

manuel.leiriaa at 2007-7-29 11:19:06 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

ok. the class looks good now. i fixed the errors. ive build it and placed in the proper TomCat-Folder. And now I get :

HTTP Status 500 -

--

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

java.lang.IllegalStateException: Cannot forward after response has been committed

org.apache.struts.action.RequestProcessor.doForward(RequestProcessor.java:1069)

org.apache.struts.action.RequestProcessor.internalModuleRelativeForward(RequestProcessor.java:1012)

org.apache.struts.action.RequestProcessor.processValidate(RequestProcessor.java:980)

org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:255)

org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)

The "forward" seems to cause the error...

Message was edited by:

nomax

nomaxa at 2007-7-29 11:19:06 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6

Does it happen in

response.sendRedirect("login.jsp");

Manuel Leiria

manuel.leiriaa at 2007-7-29 11:19:06 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 7

cant tell that... can i find it in the tomcat logs?

ive rewritten your class, because i dont have a login procedure.

it works now without errors - but it doesnt catch the jsp access....

public class CustomRequestProcessor extends RequestProcessor {

protected boolean processPreprocess(HttpServletRequest request, HttpServletResponse response) {

boolean continueProcessing = true; //Assume that everything is cool

try{

//Get the o request path

String path = processPath(request, response);

if(path.endsWith((String)"jsp")){

//U user = (UserVO)session.getAttribute("user");

//if(session== null){

response.sendRedirect("login.jsp");

continueProcessing = false;

//}

}

}catch(Exception e){

continueProcessing = false;

}

return continueProcessing;

}

}

nomaxa at 2007-7-29 11:19:06 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 8

It seems ok.

try this link to see if it has something that I forgot to tell you

http://www.onjava.com/pub/a/onjava/2004/11/10/ExtendingStruts.html

manuel.leiriaa at 2007-7-29 11:19:06 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 9

I debugged a while and found out:

String path = processPath(request, response);

it only returns the Path of the application: in my case: /login

how do I get the whole path from the browser?

nomaxa at 2007-7-29 11:19:06 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 10

> I debugged a while and found out:

>

> String path = processPath(request, response);

>

> it only returns the Path of the application: in my

> case: /login

>

> how do I get the whole path from the browser?

Try String path = reques.getContextPath + processPath(request, response);

manuel.leiriaa at 2007-7-29 11:19:06 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 11

String path = request.getContextPath() + processPath(request, response);

the log output: JSPPATH : --> /Login/login

:(

nomaxa at 2007-7-29 11:19:06 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 12

You need HttpServletRequest#getRequestURL().

BalusCa at 2007-7-29 11:19:06 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 13

getting better :

StringBuffer sb = new StringBuffer();

sb = request.getRequestURL();

I get the whole path BUT he always returns the .do extension. even if i type in a jsp ending... strange...

JSPPATH : http://127.0.0.1/Login/login.do

nomaxa at 2007-7-29 11:19:06 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 14

Why do you need the full path?

public class CustomRequestProcessor extends RequestProcessor {

protected boolean processPreprocess(HttpServletRequest request, HttpServletResponse response) {

boolean continueProcessing = true; //Assume that everything is cool

try{

//Get the request path

String path = processPath(request, response);

response.sendRedirect("login.jsp");

continueProcessing = false;

}

}catch(Exception e){

continueProcessing = false;

}

return continueProcessing;

}

}

What happens when you use this code?

Manuel Leiria

manuel.leiriaa at 2007-7-29 11:19:06 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 15

It returns only the path to the application, but not the entire path from the browser. The consequence ist: i cant check for *.jsp files with it

nomaxa at 2007-7-29 11:19:11 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...