| Author |
Problem in response.sendredirect
|
jinesh parikh
Ranch Hand
Joined: Apr 20, 2007
Posts: 48
|
|
Hi All, I am using JSP2.0. i have created one simple application which takes three parameters from the jsp and called the servlet.Servlet process them and sending the user to the new jsp page.i have deployed my application to the jboss4.2.1-GA server. Here is the jsp code which calls the servlet index.jsp <%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> </head> <body> This is my JSP page. <br> Value of the admin email ${initParam.adminemail}<br/> <form name="tstfrm" method="get" action="servlet/SimpleServlet"> Name:<input type="text" name="name"><br/> empId:<input type="text" name="empid"><br/> address:<input type="text" name="address"><br/> <input type="submit" value="enterhere"><br/> </form> </body> </html> Here is the servlet which is called on submit of the above page. package com; import java.io.IOException; import java.util.ArrayList; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import beans.Person; public class SimpleServlet extends HttpServlet { /** * Constructor of the object. */ public SimpleServlet() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("\n Inside the DOGet Method "); response.sendRedirect("/jsp/finalresult.jsp"); } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.sendRedirect("/jsp/finalresult.jsp"); /* System.out.println("\n Inside the DOPost Method "); Person p=new Person(); p.setName(request.getParameter("name")); p.setAddress(request.getParameter("address")); p.setEmpid(Integer.parseInt(request.getParameter("empid"))); //Checked for the EL parameters Using the HashMap HashMap mp=new HashMap(); mp.put("personinside", p); HashMap mp1=new HashMap(); mp1.put("personoutside", mp); //Ended Here ArrayList arr=new ArrayList(); Person p1=new Person(); p1.setName("jineshparikh"); Person p2=new Person(); p2.setName("abhijitparlikar"); arr.add(p1); arr.add(p2); request.getSession(false).setAttribute("arraylist", arr); request.getSession(false).setAttribute("person", p); request.getSession(false).setAttribute("jinesh", "person"); System.out.println("\n Setting the attribute and move to the result page 123$$$$");*/ //request.getRequestDispatcher("/finalresult.jsp").forward(request, response); //response.sendRedirect("/jsp/finalresult.jsp"); } /** * Initialization of the servlet. <br> * * @throws ServletException if an error occurs */ public void init() throws ServletException { // Put your code here } } but it is not redirecting the page to the finalresults.jsp.It is showing the wrong URL in the address bar http://localhost:8080/jsp/finalresults.jsp instead of the following http://localhost:8080/SCWCD/jsp/finalresults.jsp As per my knowledge in the response.sendRedirect() function if we specify the forward slash then it would autommatically append the context URL to the string we specified. if the above is true then what is the problem why it is not showing that page? can anybody please tellme the reason for this?
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
use response.sendRedirect("./jsp/finalresult.jsp");//use "." in front of "/"
|
 |
jinesh parikh
Ranch Hand
Joined: Apr 20, 2007
Posts: 48
|
|
thanks dear seetharaman . But it is stil not working it is keep showing that page is not found.Even though in the web application the page is on the same path. Regards Jinesh Parikh.
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
did you compile the servlet? please,can you paste your web application directory structure? before that you just try this response.sendRedirect("jsp/finalresult.jsp"); [ October 26, 2008: Message edited by: seetharaman venkatasamy ]
|
 |
jinesh parikh
Ranch Hand
Joined: Apr 20, 2007
Posts: 48
|
|
hi seetharaman , Its working i forgot to compile the servlet. can you tell me when i need to use the . operator and when not? what was the problem with my code? Regards Jinesh Parikh.
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56204
|
|
Please be sure to use UBB code tags when posting code to the forums. Unformatted code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please read this for more information. You can go back and change your post to add code tags by clicking the . Also, the "." is not valid in a redirect URL. But back to the problem, s the page showing correctly? If so, what makes you feel that the URL is wrong? If not, what is the structure of the web app -- particulary, where is the JSP stored?
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
jinesh parikh
Ranch Hand
Joined: Apr 20, 2007
Posts: 48
|
|
Dear Bear Bibeault Here is the Directory Structure of my Web Application. SCWCD | | WEBCONTENT | | | | | | | | JSP | | | | | | | | finalresults.jsp | WEB-INF | | | | | | | | classes | | | | | <compiled classes> | lib || |<All the libraries are here> index.jsp My Servlet SimpleServlet is called when you submit the page from the index.jsp.Inside the servlet there is a code for the redirecting to the finalresults.jsp Regards Jinesh Parikh
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56204
|
|
|
The root of the web application is the folder within which WEB-INF is contained. Higher-level folders are not part of the web applications.
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
Originally posted by Bear Bibeault: Also, the "." is not valid in a redirect URL.
but Bear, it is working fine in my project
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56204
|
|
|
The "." represents "current folder". What is that in relation to your servlet? It has no defined meaning and should not be used, even if it appears to be accidentally working. You should be using a server-relative URL for resources within the same application, and absolute URLs for foreign resources.
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
Thanks Bear,i understand
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56204
|
|
|
Cool. Hopefully that will save you from weird errors that are hard to diagnose in the future.
|
 |
jinesh parikh
Ranch Hand
Joined: Apr 20, 2007
Posts: 48
|
|
Thanks for the Help guys!! I am finally able to get my answer by reading material on the following link http://faq.javaranch.com/java/RelativeLinks Regards Jinesh Parikh.
|
 |
 |
|
|
subject: Problem in response.sendredirect
|
|
|