Results 1 to 2 of 2
  1. #1
    davis123877 is offline Member
    Join Date
    Feb 2015
    Posts
    9
    Rep Power
    0

    Default Having trouble pulling request parameter from html form

    Here is the code for my html form page:

    <html><body>
    <h1>Enter Username</h1><br />
    <form method="POST" action="CookieTest.do">
    <p>
    <label>Username</label>
    <input type="text" name="username" />
    </p>
    <input type="submit">
    </form></body></html>

    I also have the CookieTest.do mapped correctly in my deployment descriptor. Here is the code for the servlet:

    package com.example;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.*;

    public class CookieTest extends HttpServlet {
    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException {

    response.setContentType("text/html");
    String name = request.getParameter("username");
    Cookie cookie = new Cookie("username", name);
    cookie.setMaxAge(30*60);
    response.addCookie(cookie);

    PrintWriter out = response.getWriter();
    out.println(name);

    //RequestDispatcher view = request.getRequestDispatcher("/CheckCookie.do");
    //view.forward(request, response);
    }
    }

    When I navigate to the site and enter the username, the servlet loads and everything works fine except null is printed instead of the username. Is my form wrong or what? How do I pull the username using a request parameter in my servlet? I am not really concerned with the cookies right now, just being able to pull the username from the form and outputting it.

  2. #2
    davis123877 is offline Member
    Join Date
    Feb 2015
    Posts
    9
    Rep Power
    0

    Default Re: Having trouble pulling request parameter from html form

    Oops never mind I got it figured out. I forgot to use a slash in the sub it button.

Similar Threads

  1. Replies: 8
    Last Post: 10-15-2011, 11:41 PM
  2. Request Form
    By Aman22 in forum New To Java
    Replies: 1
    Last Post: 04-07-2011, 10:27 AM
  3. Replies: 6
    Last Post: 04-06-2011, 12:39 PM
  4. Form Parameter Undefined
    By macs_80 in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 06-29-2010, 02:26 PM
  5. Replies: 10
    Last Post: 08-12-2009, 04:02 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
  •