Reputation: 303
I am trying to get form parameters from a GET request. The html code is below.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>DesignMatch. Matching the best clients and best designers.</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript" src="jquery.backstretch.js"></script>
<script type="text/javascript" src="design_match.js"></script>
<link rel="stylesheet" type="text/css" media="all" href="design_match.css" />
</head>
<body>
<div id="matte_bg"></div>
<div id="entire_wrapper">
<div class="divModule" id="section_head_1">
<p class="primaryPageText">
Clients
</p>
<div class="url-bar">
<form name="clientUrls" action="SaveDatabase2" target="_blank" method="get">
<input type="text" class="url-box" id="url-box1" name="name1" size="20" value="Paste a link to a website that has the design qualities you want.">
<a id="add-url" href="#">Add</a>
<a id="del-url" href="#">Remove</a>
</form>
</div>
</div>
<div class="divModule" id="section_head_2">
<p class="primaryPageText">
Designers
</p>
<div class="portfolio-bar">
<form name="designerUrls" action="SaveDatabase2" target="_blank" method="get">
<input type="text" class="portfolio-box" id="portfolio-box1" name="name1" size="20" value="Paste a link to a website you designed.">
<a id="add-portfolio" href="#">Add</a>
<a id="del-portfolio" href="#">Remove</a>
</form>
</div>
</div>
<div class="divModule">
<div class="filterSentence">
<ul class="primaryPageText">
<li>
To me,
</li>
<li class="budgetTypeFilter">
<a href="#" id="budgetToggle">[what price]</a>
<div id='budgetFilter' style="display:none" class="budgetTypeMenu">
<a href="/500-under">$500 and under</a>
<a href="/500-to-1000">$500-$1,000</a>
<a href="/1000-to-2500">$1,000-$2,500</a>
<a href="/2500-to-5000">$2,500-$5,000</a>
<a href="/5000-to-7500">$5,000-$7,500</a>
<a href="/7500-to-10000">$7,500-$10,000</a>
<a href="/10000-above">Over $10,000</a>
</div>
</li>
<li>
seems fair to pay for
</li>
<li class="budgetTypeFilter">
<a href="#" id="typeToggle">[what type of]</a>
<div id='typeFilter' style="display:none" class="budgetTypeMenu">
<a href="/basic">a basic (e.g., front-end only)</a>
<a href="/dynamic">a dynamic (e.g., a little back-end)</a>
<a href="/sophisticated">a sophisticated (e.g., lots of back-end)</a>
</div>
</li>
<li>
website.
</li>
</ul>
</div>
</div>
<div class="divModule contact-bar" id="contact">
<form name="nameForm" action="SaveDatabase2" target="_blank" method="get">
<input type="text" id="name_form" name="name_form" size="20" value="Tell us your name.">
</form>
<form name="contactForm" action="SaveDatabase2" target="_blank" method="get">
<input type="text" id="contact_form" name="contact_form" size="20" value="Enter your e-mail (no spam, ever).">
</form>
</div>
<div id="submit">
<form accept-charset="UTF-8" action="SaveDatabase2" method="get">
<input class="btn primary large" id="submit-button" name="submit" type="submit" value="✔" />
</form>
</div>
</div>
</body>
</html>
Here is the Servlet code. The parameter values I get in doPost are null. I tried changing all the get requests to post in the html and it didn't help. The doGet function is just rerouted to doPost. The two main parameters I am interested in right now are the "contact_form" and "name_form" parameters at the end of the html. Thanks again!
package web;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.mysql.jdbc.StringUtils;
import java.sql.*;
import java.io.*;
import java.util.*;
/**
* Servlet implementation class SaveDatabase2
*/
public class SaveDatabase2 extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public SaveDatabase2() {
super();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<h1> Thank You </h1>");
//Get Info for Database
int designer=0;
String clientType="";
String name= "";
String email="";
//Get parameters to put into database
Enumeration parameterNames = request.getParameterNames();
if ((request.getParameter("designerUrls")!=null) && !request.getParameter("designerUrls").matches(".*\\w.*") ){
designer=1;
}
if (designer==1){
clientType="designer";
}else clientType="client";
name= request.getParameter("name_form");
email=request.getParameter("contact_form");
}
Upvotes: 2
Views: 5304
Reputation: 43823
I think you are misunderstanding the <form>
element. To send data from the page to the servlet you can use method="get"
or method="post"
- both still send the data one way (client to server). For the difference see When do you use POST and when do you use GET?
The <form>
can encompass many <input>
s and but the crucial part that is missing from your code is that the <form>
that is submitting (the one with the <input type="submit"/>
) does not contain any of the <input>
elements you are expecting. They are in other <form>
s which are not submitted. Only the <form>
that contains the submit button is submitted.
You should use one <form>
around all your fields and submit button.
Upvotes: 0
Reputation: 9266
First of all, I think you should leave doPost
and doGet
as 2 different methods. doPost
will automatically be triggered if you set the attribute method
of your form to post
.
Besides, your form should be like this:
<form name="myForm" accept-charset="UTF-8" action="SaveDatabase2" target="_blank" method="post">
<input type="text" id="name_form" name="name_form" size="20" value="Tell us your name.">
<input type="text" id="contact_form" name="contact_form" size="20" value="Enter your e-mail (no spam, ever).">
<input class="btn primary large" id="submit-button" name="submit" type="submit" value="✔" />
</form>
In your question, the submit button is in its own form which does not contain the 2 input text fields: name_form
and contact_form
. That's why you got the null
values.
Upvotes: 3