I have a simple form
form action="email.jsp" method="post"
<label for="firstname">Your Name: </label>
input type="text" id="name"<br/>
<label for="email">Your Email: </label>
input type="text" id="address"<br/>
<label for="message">Message: </label>
textarea size="30" rows="4" class="expand" id="comments"</textarea<br/>
input type="submit" value="Send" input type="reset"
/form
and am posting to a email.jsp page running in tomcat 5.5, working for another website i use that is in flash
this email.jsp is coming up with null values every time i post data to it - code below
can anyone see what i'm doing wrong?
<%# page import="sun.net.smtp.SmtpClient, java.io.*, javax.servlet.http.HttpServletResponse, javax.servlet.jsp.PageContext" %>
<%
String name = request.getParameter("name");
out.print(name);
String address = request.getParameter("address");
String comments = request.getParameter("comments");
String from="test#there.com.au";
String to="test#where.com";
try{
SmtpClient client = new SmtpClient("localhost");
client.from(from);
client.to(to);
PrintStream message = client.startMessage();
message.println("From: " + from);
message.println("To: " + to);
message.println();
message.println("Enquiry :-) from " + name + " at " + address);
message.println();
message.println("Details: "+ comments);
client.closeServer();
}
catch (IOException e){
System.out.println("ERROR SENDING EMAIL:"+e);
}
out.print("Email Sent Correctly");
%>
Your HTML is a bit garbled, but it looks like you're not specifying the "name" attribute for your inputs. The "id" attribute is good for referencing your field from a <label> or for accessing your inputs from Javascript, but the browser will not use it in the POST request.
The solution is simple, add the "name" attribute to your input elements, like this:
<input type="text" id="name" name="name" />
Related
i'm taking input values through html form and using the servlet to inserting them into the database , but when i don't put any field in the html form it is also accepting the that input . Although i've put the restrictions in my html code for the input fields .
When i tried to renter the empty fields , its shows the below error
ERROR
Duplicate entry '' for key 'users.PRIMARY
it means its accepting the user name as a empty string .
here it is my html form
<form action="userreg" method="post">
Username : <input type="text" name="username" pattern=".{3,}" title ="must contains more then 3 letters"><br/><br/>
Password : <input type="password" name="password" placeholder="password must be 8 char long one upper, lower case letter must" pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}" title="Must have 8 chars one lowercase , uppercase"><br/><br/>
FirstName: <input type="text" pattern=".{3,}" title="this field cant be empty" name="firstname"><br/><br/>
Last Name: <input type="text" pattern=".{3,}" title="this field cant be empty" name="lastname"><br/><br/>
Address : <input type="text" pattern=".{3,}" name="address"><br/><br/>
Phone No : <input type="text" pattern=".{3,}" name="phone"><br/><br/>
Email Id : <input type="text" pattern="[a-z0-9._%+-]+#[a-z0-9.-]+\.[a-z]{2,}$" name="mailid" placeholder="xyz#gmail.com" title="please enter valid mail"><br/><br/>
<input type="submit" value=" I AGREE FOR ALL TERMS & CONDITIONS ! REGISTER ME ">
</form>
here is my userRegistration servlet class
try {
Connection con = DBConnection.getCon();
PreparedStatement ps = con
.prepareStatement("insert into " + IUserContants.TABLE_USERS + " values(?,?,?,?,?,?,?,?)");
ps.setString(1, uName);
ps.setString(2, pWord);
ps.setString(3, fName);
ps.setString(4, lName);
ps.setString(5, addr);
ps.setString(6, phNo);
ps.setString(7, mailId);
ps.setInt(8, 2);
int k = ps.executeUpdate();
if (k==1) {
RequestDispatcher rd = req.getRequestDispatcher("Sample.html");
rd.include(req, res);
pw.println("<h3 class='tab'>User Registered Successfully</h3>");
} else {
RequestDispatcher rd = req.getRequestDispatcher("Sample.html");
pw.println("<h3 class='tab'>Registration failed !, please enter valid details</h3>");
rd.include(req, res);
pw.println("Sorry for interruption! Register again");
}
} catch (Exception e) {
e.printStackTrace();
}
That is the expected behavior. You may use HTML input elements attributes like „required“ and some frontend libraries to assist/enforce the presence of values, but in the end all validation needs to be done in the backend.
Because aside using the browser‘s form submit function, one can still send a malicious HTTP request using cURL or SoapUI, bypassing all frontend validations.
I would like to ask how can I use the same submit for 2 the html forms in one page. I would like to have the same action for both of the forms and thus the same servlet to handle the reuqest.
<form action = "add">
Enter first number: <input type = "text" name = "num1"><br>
Enter second number: <input type = "text" name ="num2"><br>
</form>
<br>
<br>
<form action = "add">
Enter Third: <input type = "text" name = "num1"><br>
Enter fourth: <input type = "text" name ="num2"><br>
<input type = "submit">
</form>
</body>
1 You can to enter one hidden parameter like
<input type="hidden" name="fieldHidden" value="formOne/formTwo">
in the jsp file for each form.
On servlet you will pass value of parameter into new variable
String form = request.getParameter("fieldHidden");
if(form.equals("formOne"){
//process create logic for form 1
}
else if(form.equals("formTwo")) {
//process create logic for form 2
}
for example :
in your jsp file :
<form action = "add">
<input type="hidden" name="fieldHidden" value="formOne">
Enter first number: <input type = "text" name = "num1"><br>
Enter second number: <input type = "text" name ="num2"><br>
<input type = "submit">
</form>
<br>
<br>
<form action = "add">
<input type="hidden" name="fieldHidden" value="formTwo">
Enter Third: <input type = "text" name = "num1"><br>
Enter fourth: <input type = "text" name ="num2"><br>
<input type = "submit">
</form>
On servlet you will pass value of parameter into new variable
String form = request.getParameter("fieldHidden");
if(form.equals("formOne"){
//process create logic for form 1
}
else if(form.equals("formTwo")) {
//process create logic for form 2
}
Hope this helps :)
Home.jsp Post aid to Edit.jsp
<input type="button" value="修改" onclick="location.href='Edit.jsp?aid=<%=art.getAsId()%>'"/>
Edit.jsp Get aid.
String aid = request.getParameter("aid");
out.print(aid);
if (aid == null)
response.sendRedirect("Home.jsp");
else {
IAsset ad=new AssetDao();
AssetName art=ad.getInfo(Integer.parseInt(aid));
}
EditControl.jsp
but in this page, it always tell me aid =null.
request.setCharacterEncoding("utf-8");
AssetName art= new AssetName();
art.setAsId(Integer.parseInt(request.getParameter("aid")));
art.setUID(request.getParameter("UID"));
art.setAsnu(request.getParameter("asnu"));
art.setAsnm(request.getParameter("asnm"));
art.setAste(request.getParameter("aste"));
art.setSpec(request.getParameter("spec"));
And Report
java.lang.NumberFormatException: null
<input type="button" value="修改" onclick="location.href='Edit.jsp?aid=<%=art.getAsId()%>'"/>
In this line you missed to give name of the input field
so you need to change it like this
<input type="button" name="aid" value="修改" onclick="location.href='Edit.jsp?aid=<%=art.getAsId()%>'"/>
and change parsing code like this
AssetName art=ad.getInfo(Integer.valueOf(aid));
Why is my method does not work?
My Java code:
#POST
#Path("/request=PostStage")
#Produces(MediaType.APPLICATION_JSON)
public String getStagePOST(#QueryParam("fn")String fn,
#QueryParam("tn")String tn,
#QueryParam("stat")String stat,
#QueryParam("length")String length,
#QueryParam("lon")String lon,
#QueryParam("lat")String lat,
#QueryParam("crgw")String crgw,
#QueryParam("lane")String lane) throws SQLException{
return "Lat: " + lat + " lon: " + lon + " crgw: " + crgw;
}
My HTML code:
<form action="http://localhost:9090/services/stage/request=PostStage" method="POST">
<p>Localization:</p>
<p> fn : <input name="fn" /></p>
<p> tn : <input name="tn" /></p>
<p>stat : <input name="stat" /></p>
<p>length : <input name="length" /></p>
<p>Geoposition:</p>
<p>lon : <input name="lon" /></p>
<p>lat : <input name="lat" /></p>
<P> Other:</P>
<p>crgw : <input name = "crgw" /></p>
<p> lane : <input name="lane" /></p>
<input type="submit" value="Searchh" />
</form>
I give examples of parameters in a html page: lon - 12, lat - 12 etc.
As a result, I get:
Lat: null lon: null crgw: null
Why?
I can not find the problem :(
Very thanks for all answers .
Shouldn't there be types on the input-tags, like <input name="lon" type="text" />?
Also verify that the POST from the form really contains the parameters, the browser debugging IDE in both Chrome and Firefox is probably able to see how the POST-request looks.
Working! My mistake was to use "#QueryParam" instead "#FormParam".
I have created two jsp pages in which I am able to get the response of my input. Below are my 2 jsp pages. Also I would like to get a response from android java class to the same Index1.jsp.(For eg: If I am sending one string to Index1.jsp I would to get the response as same string).
Index.jsp
<form action="index1.jsp" method="POST">
<input type="checkbox" name="maths" checked="checked" /> Maths
<input type="checkbox" name="physics" /> Physics
<input type="checkbox" name="chemistry" checked="checked" /> Chem
<input type="submit" value="Select Subject" />
</form>
Index1.jsp
<%
Enumeration paramNames = request.getParameterNames();
while(paramNames.hasMoreElements()) {
String paramName = (String)paramNames.nextElement();
out.print("<tr><td>" + paramName + "</td>\n");
String paramValue = request.getParameter(paramName);
out.println("<td> " + paramValue + "</td></tr>\n");
}
%>