pragmus
pragmus

Reputation: 4043

Writing a simple servlet. Getting and sending data via servlet

I'm novice in servlets writing. I need to write a simple servlet, it must to input and output data via form. For example, in my servlet I input data about some car(it is consist of such attributes: car name, car size and car color). Servlet must save these data. And also, it must show saved data. Something of servlet I have done, but I still have not knowledges to complete it.

This is a html-code of page, servlet is call from it:

<!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>Cars</title>
</head>

<body><center>Cars</center>

<p>Name: </p>
<form id="form1" name="form1" method="post" action="">
  <label for="car_name"></label>
  <input type="text" name="car_name" id="car_name" />
</form>
<p>Color: </p>
<form id="form2" name="form2" method="post" action="">
  <label for="car_name"></label>
  <input type="text" name="car_color" id="car_color" />
</form>
<p>Size: </p>
<form id="form3" name="form3" method="post" action="">
  <label for="car_size"></label>
  <input type="text" name="car_size" id="car_size" />
</form>
<input name="send" type="button" value="Send" />
<input name="get_out" type="button" value="Output" />
<textarea name="output" cols="10" rows="10" readonly="readonly"></textarea>
<fieldset>
<legend>Testing Simple Servlets</legend>
<ul>
  <li><a href="carServlet">carServlet</a> The carServlet is a servlet that
      gets and posts cars' attributes data</li>
</ul>
</fieldset>
</body>
</html>

And this is a servlet-code(java), (I only begin to realize it):

package testPackage;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;

@WebServlet("/carServlet")
public class CarServlet extends HttpServlet 
{
    //Some code strings which finds items on the web-page
    //Only for example: String item = (String)getItem("car_name");
}

How to complete it in order to servlet could save and print out saved data?

Upvotes: 1

Views: 774

Answers (2)

uncleBounty
uncleBounty

Reputation: 773

HREF links -> You can get your data in the doGet method of your Servlet. FORM submits -> See your doPost method

Upvotes: 0

brso05
brso05

Reputation: 13222

package testPackage;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;

@WebServlet("/carServlet")
public class CarServlet extends HttpServlet 
{
   protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
   }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String carName = request.getParameter("car_name");
        System.out.println("Car Name:" + carName);
   }
}

Try this implement your doGet and doPost methods inside your servlet. You are submitting form via post so you can put your code there. I just forwarded the get to the post method. You can access parameters by request.getParameter("parameterName");

Upvotes: 1

Related Questions