2014-10-10 58 views
1

我是servlets寫作的新手。我需要編寫一個簡單的servlet,它必須通過表單輸入和輸出數據。例如,在我的servlet中,輸入關於某輛車的數據(它由以下屬性組成:汽車名稱,汽車大小和汽車顏色)。 Servlet必須保存這些數據。而且,它還必須顯示保存的數據。我已經做了一些servlet,但我還沒有知道完成它。編寫一個簡單的servlet。通過servlet獲取和發送數據

這是網頁的HTML代碼,servlet的是從它的呼叫:

<!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> 

這是一個servlet代碼(JAVA),(我纔開始意識到這一點):

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"); 
} 

如何完成它以便servlet可以保存和打印保存的數據?

+1

我建議您在網上尋找教程並從那裏開始。讓一個返回一些東西的servlet並不是非常困難。如果你有一個特定的問題,你可以在這裏再次提問。可能的教程:http://www.tutorialspoint.com/servlets/servlets-first-example.htm – Juru 2014-10-10 20:00:29

+1

您還應該閱讀關於HTML表單的教程。你的HTML有幾個問題:(1)應該只有一個包含所有字段的表單,而不是多個單獨的表單。 (2)頁面沒有告訴瀏覽器在哪裏發送表單(你的servlet)。表單必須具有一個作爲servlet地址的操作。 (3)沒有提交按鈕。如果您點擊其中一個按鈕,瀏覽器不知道該怎麼做。嘗試從[本教程](http://www.javascript-coder.com/html-form/html-form-tutorial-p1.phtml)開始。 – RealSkeptic 2014-10-10 20:35:01

回答

1
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); 
    } 
} 

試試這個實現您的servlet中的doGet和doPost方法。您通過郵件提交表單,以便您可以將代碼放在那裏。我只是將get轉發到post方法。您可以通過request.getParameter(「parameterName」);來訪問參數。

0

HREF鏈接 - >您可以在Servlet的doGet方法中獲取數據。 表格提交 - >請參閱您的doPost方法

+0

添加更多的細節和解釋。您也可以使用doGet作爲表單。並解釋HREF(URL)。 – brso05 2014-10-10 20:14:06