
I have to do a project for my web programming class where we create a html file that contains four text boxes, first and last name, address and zipcode, along with a submit button. along with this I need to create a java servlet file which prints out the user's inputs. I need to use the post method to pass the data from html to the java servlet and also use both doGet and doPost methods in the servlet. I think, but unfortunately I have a terrible teacher and I can't figure out the rest. So, if anyone can help me so I can see how to connect the two I would be very greatful.

Hi Friend,
Try the following code:
1)callServlet.html:
<html>
<center><h3>Form</h3></center>
<form method="post" action="http://localhost:8080/examples/Servlet">
<center>
<table>
<tr><td>First Name:</td><td><input type="text" name="firstname" size="20"></td></tr>
<tr><td>Last Name:</td><td><input type="text" name="lastname" size="20"></td></tr>
<tr><td>Address </td><td><input type="text" name="address" size="20"></td></tr>
<tr><td>Zip Code </td><td><input type="text" name="zip" size="20"></td></tr>
<tr><td><input type="submit" value="Submit" name="B1"></td><td><input type="reset" value="Reset" name="B2"></td></tr>
</table>
</center>
</form>
</html>
2)Servlet.java:
import java.io.*;
import java.sql.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Servlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String fname=request.getParameter("firstname");
out.println("Firstname: "+fname+"<br>");
String lname=request.getParameter("lastname");
out.println("Lastname: "+lname+"<br>");
String address=request.getParameter("address");
out.println("Address: "+address+"<br>");
String zip=request.getParameter("zip");
out.println("Zip Code: "+zip+"<br>");
}
public void doGett(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
}
}
3)Do servlet mapping in web.xml:
<servlet>
<servlet-name>Servlet</servlet-name>
<servlet-class>Servlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Servlet</servlet-name>
<url-pattern>/Servlet</url-pattern>
</servlet-mapping>
Note: Put you html file inside web application folder and servlet inside WEB-INF/classes folder of web application forlder.
Thanks
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.