Results 1 to 7 of 7
  1. #1
    Amal is offline Member
    Join Date
    Jan 2014
    Posts
    4
    Rep Power
    0

    Default sendredirect problem

    Hello everyone , I am beginner in J2EE, I coded a form "index.html" and a sevlet "HelloServlet" ...
    what i want to de is : If a user calls"HelloServlet" directly by its URL (copy the address in the browser) then the response will be redirected to the form index.html ... that is why I used this code

    Java Code:
    if(nom.isEmpty()&&prenom.isEmpty())
        res.sendRedirect("index.html");
    where "nom" and "prenom" are the form fields...

    but it just redirect the response to index.html when the form fields are umpty ... HELP please
    Thank you
    Last edited by Amal; 01-28-2014 at 09:09 PM.

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: sendredirect problem

    Hi Amal,

    You might know that index.html is usually serve as a default page in a webserver. Which mean that if you try to access a URL with no page name the server will return the default page, which is the index.html. As a solution you might change your form page name into form.html instead of the default index.html. Let's see what will happen.

  3. #3
    Amal is offline Member
    Join Date
    Jan 2014
    Posts
    4
    Rep Power
    0

    Default Re: sendredirect problem

    Thank you wsaryada for answering , I did what you asked me to do , but I still have the same problem ..

  4. #4
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: sendredirect problem

    Okay, maybe you have something else there. Can you post your form html and the servlet code? and what is the nom and prenom variable value coming from.

  5. #5
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,087
    Rep Power
    6

    Default Re: sendredirect problem

    I don't get the actual problem!

    what i want to de is : If a user calls"HelloServlet" directly by its URL (copy the address in the browser) then the response will be redirected to the form index.html
    If empty, show form in index.html. Got it, makes sense.

    but it just redirect the response to index.html when the form fields are umpty
    ... isn't that what you wanted just a second ago!?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    Amal is offline Member
    Join Date
    Jan 2014
    Posts
    4
    Rep Power
    0

    Default Re: sendredirect problem

    gimbal2 what I wanted to do is: not to let anyone access to the HelloServlet (by copying the servlet's URL in the browser) without going through the form .
    it's not about having the fields umpty ... My English is not good ... I hope that you could understand me .

  7. #7
    Amal is offline Member
    Join Date
    Jan 2014
    Posts
    4
    Rep Power
    0

    Default Re: sendredirect problem

    wsaryada

    HelloServlet

    Java Code:
    public class HelloServlet extends HttpServlet {
    //@Override
    public void service ( HttpServletRequest req, HttpServletResponse res) throws ServletException,
    IOException {
    //res. setContentType("text/html");
    PrintWriter out = res. getWriter();
    String nom = req.getParameter("nom");
    String prenom = req.getParameter("prenom");
    if(nom.isEmpty()&&prenom.isEmpty())
        res.sendRedirect("Form.html");
    else
    {
    out.println("<HTML><BODY>");
    out.println("<h1> Hello!</h1> <p>" + nom+ " "+prenom + "</p>");
    out.println("</BODY></HTML>");
    }}}
    Form.html

    XML Code:
    <P> veuillez remplir le foormulaire SVP
    <FORM action="/TP01_J2EE/HelloServlet" method="post" >
    <P> Veuillez saisir vos informations 
    <P> nom: 
    <INPUT type="text" name="nom" maxlength="20" size="12">
    <P> prenom: 
    <INPUT type="text" name="prenom" maxlength="20" size="12">
    <P> Thank you! <INPUT TYPE="SUBMIT" value="ok">
    </FORM>

Similar Threads

  1. sendRedirect to a a framed page
    By adammyth in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 03-17-2010, 09:58 PM
  2. sendredirect not working
    By ranga in forum Java Servlet
    Replies: 0
    Last Post: 02-11-2008, 10:48 AM
  3. response.sendRedirect(url)
    By mutuah in forum Advanced Java
    Replies: 2
    Last Post: 08-07-2007, 01:56 PM
  4. help with response.sendRedirect method
    By katie in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 08-07-2007, 02:22 AM
  5. response.sendRedirect problem
    By christina in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 08-03-2007, 05:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •