aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes request parameter lost with sendredirect() ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "request parameter lost with sendredirect() ?" Watch "request parameter lost with sendredirect() ?" New topic
Author

request parameter lost with sendredirect() ?

amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
do request parameter lost with sendredirect() ?

actually i am passing parameter from html to servlets and its working fine...
but when i call jsp and use

request.sendRedirect("abc.do");

its not working where abc.do is naming convention used in DD


its giving java.lang.NullPointerException
com.phoneDir.getPhone(testing.java:44)
com.testing.doGet(testing.java:22)


anyidea ?


Thanks and Regards, Amit Taneja
Kedar Dravid
Ranch Hand

Joined: May 28, 2004
Posts: 333

do request parameter lost with sendredirect() ?

Yes.


but when i call jsp and use
request.sendRedirect("abc.do");


It should be response.sendRedirect()
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
no kedar ..but whom should it redirect ?

we must specify something i.e some url to which it redirect ..?

so we can't use sendRedirect(); when data is comming from form ?
Kedar Dravid
Ranch Hand

Joined: May 28, 2004
Posts: 333
It should be repsonse.sendRedirect("someURL");
shiva viswanathan
Ranch Hand

Joined: Aug 12, 2004
Posts: 152
Hi Amit ,

When you pass parameters params to the servlet then the url submitted will be
ServletName?param1=param1value¶m2=param2value
and thats how we get the parameter values in the servlet

However when you do a redirect that information will be lost since
you are sending only ServletName . Hence you need to append the
queryString to the Servlet Name before doing a redirect

You can get the queryString by doing request.getQueryString()

Catch You Later
Shiva
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
i putted my html on web application main directroy..
and one jsp..

I am calling jsp file from html and passing some parameter..

which jsp is redirecting to servelt name testing in com(package)
i.e classes/com/testing

I have put name of testing.class as abc.do in DD

so what should i put in sendRedirect(url) so i will call servlet ?


i tried putting SendRedirect("abc.do") , SendRedirect("/abc.do")
SendRedirect("/WEB-INF/classes/com/testing")

but its not working ?

any idea ?
sawan parihar
Ranch Hand

Joined: Aug 24, 2004
Posts: 250
Hi Amit,
The sendRedirect("URL") will make the browser request the new URL that you have specified . That means you provide the URL as whatever you have given in the <servlet-mapping> tag and it should work.


Sawan<br />SCJP,SCWCD,SCBCD<br /> <br />Every exit is an entry somewhere.
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
its not working

<web-app>
<servlet>
<servlet-name>testinner</servlet-name>
<servlet-class>com.testing</servlet-class>

<init-param>
<param-name>email</param-name>
<param-value>taneja.amit@gmail.com</param-value>
</init-param>

</servlet>

<servlet-mapping>
<servlet-name>testinner</servlet-name>
<url-pattern>/abc.do</url-pattern>
</servlet-mapping>

</web-app>



so in above servlet-mapping tag..
i specified but not wroking ???
Darya Akbari
Ranch Hand

Joined: Aug 21, 2004
Posts: 1855
Hi Amit,

can't you post all files from your web app here or let us download it, so we can get a clearer picture. Maybe there is something else you missed, because your web.xml looks fine

Regards,
Darya


SCJP, SCJD, SCWCD, SCBCD
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
i think its not nessary to post all code..

please make any eg. where you call "servlet" sendRedirect(url) from jsp

and let me know it will enough

regards,
amit
Darya Akbari
Ranch Hand

Joined: Aug 21, 2004
Posts: 1855
Hi Amit,

here an example:



hfsj-ch6-beerV2 is the web app directory.

Supposed you deployed your app correcty, you should try it with sendRedirect("/YOUR_WEB_APP_DIR/abc.do") in your case.

By the way, Kedar is right, take care it's response.sendRedirect(...) AND NOT request.sendRedirect(...)

Regards,
Darya
[ July 27, 2005: Message edited by: Darya Akbari ]
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
its not working ...here is the all the code...

web-application name : jsp_prac (i.e webapps->jsp_prac)

1.
file name = index.html place=jsp_prac/index.html ...called fisrt bye
http://127.0.0.1:8080/jsp_prac/index.html

<html>
<head>
<title>
application form
</title>
</head>
<body>
<b>
Application Form : </b>

<br>
<Br>
<form method=get action="test1.jsp">
Name : <input type=text name=name> <br>
Emp id : <input type=text name=empid><br>

<input type=submit >
</form>

</body>
</html>
------------------------------------------

2. file name = test1.jsp place= jsp_prac/test1.jsp

<html>

<% response.sendRedirect("/jsp_prac/com/abc.do?name=amit&empid=22"); %>
<BR>
<% //out.println(request.getParameter("empid")); %>

</html>

------------------------

3. file name =web.xml place= jsp_prac/web-inf/web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
<servlet>
<servlet-name>testinner</servlet-name>
<servlet-class>com.testing</servlet-class>

<init-param>
<param-name>email</param-name>
<param-value>taneja.amit@gmail.com</param-value>
</init-param>

</servlet>

<servlet-mapping>
<servlet-name>testinner</servlet-name>
<url-pattern>/abc.do</url-pattern>
</servlet-mapping>

<context-param>
<param-name>email_id</param-name>
<param-value>golutaneja@yahoo.com</param-value>

<param-name>name</param-name>
<param-value>amit</param-value>

</context-param>

<listner>
<listner-class>com.MyContextListner </listner-class>
</listner>

</web-app>

----------------------------
4. file name : testing.java(.class)
place: web-inf/classes/com/testing.java


package com;
import com.phoneDir;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;


public class testing extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException,ServletException
{
res.setContentType("text/html");

PrintWriter out =res.getWriter();
out.println("<html> Hack the matrix </html>");
out.println(req.getParameter("name"));
out.println(req.getParameter("empid"));

com.phoneDir ph= new com.phoneDir();
List phone_no =ph.getPhone(req.getParameter("name"));
String emailid =getServletConfig().getInitParameter("email");
phone_no.add(emailid);
req.setAttribute("phones", phone_no);
RequestDispatcher rp= req.getRequestDispatcher("view.jsp");

rp.forward(req,res);

/* Iterator it= phone_no.iterator();
while(it.hasNext())
{ out.println("<br> "+ it.next());
}
}
*/
}
}

----------------------------------
5. filename= phoneDir.java(class)
place= web-inf/classes/com/phoneDir.class

package com;

import java.util.*;

public class phoneDir
{
public List getPhone(String name)
{
List phone=new ArrayList();

if (name.equals("amit"))
{
phone.add("9873085014");
phone.add("01122721799");
}

else
{
phone.add("unknown");
phone.add("unknown");
}
return phone;
}

}

--------------------------------------------------

6. file name= view.jsp place= jsp_prac/view.jsp

<%@ page import="java.util.*" %>

<html>
<head> <title> jsp view </title> </head>

<body>


<h1 align="center"> Phone no. details </h1>
<%
List style=(List)request.getAttribute("phones");
Iterator it = style.iterator();
while(it.hasNext())
{
out.println("<br> phone :"+it.next());
}
out.println("<br> servet context email :"+getServletContext().getInitParameter("email_id"));
out.println("<br> servet context email throght config obj :"+getServletConfig().getServletContext().getInitParameter("email_id"));
out.println("<br> getMajor Version :"+getServletContext().getMajorVersion());
out.println("<br> ger server info :"+getServletContext().getServerInfo());
out.println("<br> ger server info through application :"+application.getServerInfo());
out.println(" <br> server port :"+request.getServerPort());
out.println("<br> local port :"+request.getLocalPort());
out.println("<br> remote port :"+request.getRemotePort());
out.println(" <br> context path :"+request.getContextPath());
out.println(" <br> header :"+request.getHeader("Content-Length"));
out.println(" <br> servlet info :"+ getServletInfo());
out.println(" <br> servlet config :"+ getServletConfig());
out.println(" <br>getPathTranslated:"+ request.getPathTranslated());
out.println(" <br>getServletPath :"+ request.getServletPath());
out.println(" <br>getRequestURL() :"+ request.getRequestURL() );
out.println(" <br> Path Info :"+ request.getPathInfo());
out.println(" <br> location header :"+ request.getHeader("Location"));
out.println(" <br> content-type header :"+ request.getHeader("content-type")+"<br><hr><Br> ;
application.log("hack the matrix...into log");

%>

</body>

=============================================


the following error is comming when i m tring to call test1.jsp from index.html and using sendRedirect("test1.jsp");


message /jsp_prac/testing

description The requested resource (/jsp_prac/testing) is not available.


please solve this sendredirect issue
[ July 27, 2005: Message edited by: amit taneja ]
Darya Akbari
Ranch Hand

Joined: Aug 21, 2004
Posts: 1855
Hi Amit,

it's working . However you must always take care when you type . You have lots of type mismatches. I had to make changes in each file, all of the kind of type mismatches. So take care of it.

Here it goes:

in your jsp_prac web app folder you have 3 files index.html, test1.jsp and view.jsp:

index.html:


test1.jsp:


view.jsp:


In your WEB-INF folder put this web.xml Deployment Descriptor:


In your WEB-INF/classes/com folder put the binaries (.class) of Testing.java and PhoneDir.java:

Testing.java:


PhoneDir.java:


I leave it for you to find where I made all the changes

Good Luck,
Darya
Chintan Rajyaguru
Ranch Hand

Joined: Aug 19, 2001
Posts: 341
Amit,
Why do you have a "com" in
<% response.sendRedirect("/jsp_prac/com/abc.do?name=amit&empid=22"); %>

?


ChintanRajyaguru.com
SOADevelopment.com - Coming soon!
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
Originally posted by Chintan Rajyaguru:
Amit,
Why do you have a "com" in
<% response.sendRedirect("/jsp_prac/com/abc.do?name=amit&empid=22"); %>

?


its wrong writen...
com is the pakage name under classes where testing.java and phoneDir.java is kept..

if you find solution post here... akbari sol is not working yet on my system..
[ July 27, 2005: Message edited by: amit taneja ]
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
as soon as i add listener tag in web.xml no file is accessable not even index.html through tomcat

why this happening ?

here is another file com/MyContextListener.java

package com;
import javax.servlet.*;
import java.util.*;
import com.phoneDir;

public class MyContextListener implements javax.servlet.ServletContextListener
{
public void contextInitialized(ServletContextEvent event)
{
System.out.println("context intialized");
ServletContext sc= event.getServletContext();
String args=sc.getInitParameter("name");

phoneDir ph= new phoneDir();
List phones=ph.getPhone(args);
sc.setAttribute("sc_ph",phones);
}

public void contextDestroyed(ServletContextEvent event)
{
System.out.println("contextDestroyed");

}

}


why this is happening..


and can anybody gives me the link to understand what happening on tomcat prompt ..there are several msgs that prints when it starts and when some event occours ..
[ July 27, 2005: Message edited by: amit taneja ]
Darya Akbari
Ranch Hand

Joined: Aug 21, 2004
Posts: 1855
Hi Amit,

here is the result from your own web app you recently sent to me :



I tested it on IE and Opera with http://localhost:8080/jsp_prac/index.html and you can see that it works fine

What is your browser you use ? Install your browser again or
install a new one and test again.

Regards,
Darya
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
akbari ..the above problem got solved..

pls solve MyContextListner problem..
i.e whenver i put <listener> tag

<listener>
<listener-class>com.MyContextListener</listener-class>
</listener>


it don't show any page not even html page


thanx
[ July 28, 2005: Message edited by: amit taneja ]
Darya Akbari
Ranch Hand

Joined: Aug 21, 2004
Posts: 1855
Hi Amit,

I don't have the time right now, since I have to go on with the SCWCD preparation .

But generally, if you want lots of people access your code so that they can take a deeper look in it, I really recommend that you go and setup up your own web site (Yahoo!GeoCities does it for free) and upload your project to your new web site.

Once you did that, you can set a link from within your JavaRanch post to your web site's project file.

By this, you have given a convenient way to other people to access your code and your chances to obtain valuable help rise dramatically .

So think about it.

Regards,
Darya
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: request parameter lost with sendredirect() ?