Hi
I am using struts in my application...and I want to run secured pages to under https and other page like login page under http..
for that i included a redirect to HTTPServlet
it works well in tomcat 5.0 but not in 5.5 ... Here the problem is when i try to convert from the HTTP to HTTPS in tomcat 5.5 instead of showing the struts action path in the URL as (http://localhost:8080/gensource.do ) it is passing the parameter in the URL as (http://localhost:8080/web/jsp/common/gensource_home.jsp) i.e., the location of the jsp file and not logging into the application
But if i try to run complete application in the HTTPS then it works well even in tomcat 5.5 ...
problem occurs only when i try to redirect from HTTP to HTTPS ... can any one help me it this regard
Thanks for your reply...
I tried to use request.getURI() and request.getServlet() methods...but those methods also giving whole path...
Harika emm
Greenhorn
Joined: May 28, 2008
Posts: 8
posted
0
Thanks for your reply...
I tried to use request.getRequestURI() and request.getServletPath() methods...but those methods also giving whole path...
Can you tell me how to ActionMapping's getPath() method to get action path...actually i wrote this method in normal java file..and calling this method in all jsp pages..
/** This object is used by JSP pages in order to redirect the user from
the
* https login screen to the url they requested. The only catch is that
the
* URL the user requested required http. Therefore this object will
* change the protocol of the URL back to http instead of https.
*
* @author psantos
*/
public class JSPHttpRedirect
{
public final static boolean DEBUG = false;
static Logger log = GetDataBaseConnection.log;
/** Creates a new instance of JSPHttpRedirect */
public JSPHttpRedirect()
{
}
/** This will redirect the user from the http login page to
* the https page they are supposed to go to. If the request url
* uses https then nothing is done.
* @param request The object that represents the client request.
* @param response The object that representes the client response.
*/
public static void redirectToHTTPS(HttpServletRequest request,
HttpServletResponse response)
{
System.out.println("issecure==>"+request.isSecure());
if(!request.isSecure())
{
if(isMozillaBrowser) // mozilla perform a redirect.
{
try
{
response.sendRedirect(response.encodeRedirectURL(reqURL));
}
catch(Exception e)
{
e.printStackTrace();
}
}
else // internet explorer set the location header field
{
reqURL = response.encodeURL(reqURL);
response.setHeader("Location",reqURL);
}
}
}
}
/** This will redirect the user from the https secured pages to
* the http page they are supposed to go to. If the request url
* uses https then nothing is done.
* @param request The object that represents the client request.
* @param response The object that representes the client response.
*/
public static void redirectToHTTP(HttpServletRequest request,
HttpServletResponse response)
{
log.info("issecurehttp==>"+request.isSecure());
// we came from a secure login but we intended to use http.
// therefore we need to convert the URL to use http protocol
// depending on the browser do the appropriate thing to
// avoid a popup being shown to the user that they were
// redirected from an https to an http URL.
if(isMozillaBrowser) // mozilla perform a redirect.
{
try
{
response.sendRedirect(response.encodeRedirectURL(reqURL));
}
catch(Exception e)
{
e.printStackTrace();
}
}
else // internet explorer set the location header field
{
reqURL = response.encodeURL(reqURL);
response.setHeader("Location",reqURL);
try
{
/** This method determines the type of browser the request is from.
* If the browser is a netscape or mozilla browser return true.
* If it's internet explorer return false.
* @param request The object that represents the client request.
*/
public static boolean isMozillaBrowser(HttpServletRequest request)
{
// this snipet of code was found over the web on 11/09/2005
// http://hotwired.lycos.com/ // webmonkey/01/22/index3a_page4.html?tw=programming
Also, one thing I noticed that, you can implemented this logic in JavaScript by using window.location = newURL. Why do you want to use Java Code in JSP, a scriptlets, its a bad programming practice, can't you just use JavaScript.