Link to home
Start Free TrialLog in
Avatar of JosephRalph
JosephRalph

asked on

How to forward a page without the parameters recived.

I have an jsp page that calls a servlet it process some data from the buttons in the jsp, now I whant the servlet to forward the action to the same jsp but without the parameters in the querystring.

Example
if I do this
*******************************************************
response.sendRedirect("somepage.jsp");

// it works but I also need so set an atribute but i dont know how to do that.
********************************************************
also if i do this
********************************************************************
String Name = request.getParameter("NAME");
request.setAttribute("weCan", s);
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/somepage.jsp");
dispatcher.forward(request,response );

//The problem here is that I dont whant the parameter NAME to be include in the request.
//Create problems if I Relod the page
********************************************************************
In sumarry I need to erase parameters and set Attributes


SOLUTION
Avatar of kennethxu
kennethxu

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JosephRalph
JosephRalph

ASKER

hy,

I try the overrrivde parameter method #2 ("/somepage.jsp?NAME="), but it dosn't change the value of the parameter maybe because I have more that one parameter like NAME=Jose&LASTNAME=Howard&ect........
it should work! can you show us your code?

in addition, try out 2 jsps below:

x.jsp:
<%
    // pageContext.forward("y.jsp?NAME=");
    request.setAttribute("weCan", "Yes" );
    RequestDispatcher dispatcher = application.getRequestDispatcher("/y.jsp?NAME=");
    dispatcher.forward(request,response );
%>

y.jsp:
<html>
    ServletPath: <%=request.getServletPath()%><p>
    RequestURL: <%=request.getRequestURL()%><p>
    RequestURI: <%=request.getRequestURI()%><p>
    NAME: <%=request.getParameter("NAME")%><p>
    LASTNAME: <%=request.getParameter("LASTNAME")%><p>
    weCan: <%=request.getAttribute( "weCan" )%><p>
</html>

Result: http://localhost:8080/x.jsp?NAME=Jose&LASTNAME=Howard

ServletPath: /y.jsp
RequestURL: http://localhost:8080/x.jsp
RequestURI: /y.jsp
NAME:
LASTNAME: Howard
weCan: Yes
the code for the jsp.

"CreatExam.jsp"

<form action="NewServlet" method="get" >   
    <input type="text"  Name="Name" size="19" >   
    <select Name ="ClassDrop" > 
    <option></option>
    <c:forEach var="row" items="${queryresults2.rows}">
        <option><c:out value="${row.Nombre}" /></option>
    </c:forEach>
    </select>
    <select  Name ="TopicDrop">
    <option></option>
    <c:forEach var="row" items="${queryresults.rows}">
        <option><c:out value="${row.Nombre}" /></option>
    </c:forEach>
    </select>
    <input  type="submit" value="  Go  " > 
    </form>

here is the adress before I call the servlet http://localhost:8084/Acess/CreateExam.jsp?


now the code of the servlet

"NewServlet.java"

String Name = request.getParameter("Name");
String Class = request.getParameter("ClassDrop");
String Class = request.getParameter("TopicDrop");

RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/CreateExam.jsp?Name=");
dispatcher.forward(request,response );

now the adress after
http://localhost:8084/Acess/NewServlet?Name=first&ClassDrop=Matematica&TopicDrop=algebra

I try to understad what you said to me see if i'am rigth
the first time I press the button in the jsp it goes to the servlet thenit return to the jsp and it has change to null the parameters Name,ClassDrop,TopicDrop. But this change is not reflected in the adress http://localhost:8084/Acess/NewServlet?Name=first&ClassDrop=Matematica&TopicDrop=algebra
but if I press the reload in the browser then this data is reinserted in the DB.

   
as i said before if RequestDispatcher is used the switch happens at the server side only so the client browser cannot know what address of the jsp/html it has got back... so u use Response.sendRedirect from your second jsp.....
Both of you help me so I divide the points 50 to kiranhk  and 75 for kennethxu that you both.
glad to know you problem is resolved.