2016-09-27 84 views
3

所以我有一些前端Web開發經驗和OOP(使用Java)的豐富經驗,但我從來沒有從HTML表單處理用戶輸入和做有這些數據的東西。由於我在Java方面有很多經驗,我認爲這將是最好的語言。我想要做的是從表單中收集用戶數據(基本內容:名稱,地址等)並將其處理到Java文件中。現在,我不認爲數據甚至被髮送到Java文件,因爲當我點擊「提交」按鈕時,什麼也沒有發生。我在這裏做錯了什麼?HTML不發送數據到Java文件

的index.html:

<!DOCTYPE html> 

<html> 

<head> 
<meta charset="utf-8"> 
<meta http-equiv="X-UA-Compatible" content = "IE=edge"> 
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"> 
<title>Lob Application</title> 

<link href = "bootstrap.min.css" rel = "stylesheet"> 
<link href = "stylesheet.css" rel = "stylesheet" > 
</head> 

<body> 
<div id = formInput class = "container-fluid"> 
<form name = "submitDataForm" method = "post" action = "processData"> 
    Name:<br> 
    <input type = "text" name = "name"><br> 
    Address Line 1:<br> 
    <input type = "text" name = "addressLineOne"><br> 
    Address Line 2:<br> 
    <input type = "text" name = "addressLineTwo"><br> 
    City:<br> 
    <input type = "text" name = "city"><br> 
    State:<br> 
    <input type = "text" name = "state"><br> 
    Zip Code:<br> 
    <input type = "text" name = "zip"><br> 
    Message:<br> 
    <input type = "text" name = "message"><br> 
    <input id = "submit" type = "submit" value = "Submit" 
</form> 
</div> 

<!-- jQuery (necessary for Bootstrap's JavaScript plugins) --> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> 
<!-- Include all compiled plugins (below), or include individual files as needed --> 
<script src="bootstrap.min.js"></script> 
</body> 
</html> 

processData.java:

import java.io.*; 
import javax.servlet.*; 
import javax.servlet.http.*; 

@WebServlet("/processData") 
public class processData extends HttpServlet{ 
public static void main(String[] args){} 

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{ 
String name = req.getParameter("name"); 
String address1 = req.getParameter("addressLineOne"); 
String address2 = req.getParameter("addressLineTwo"); 
String city = req.getParameter("city"); 
String state = req.getParameter("state"); 
String zip = req.getParameter("zip"); 
String message = req.getParameter("message"); 

System.out.println("city: " + city); 

PrintWriter writer = resp.getWriter(); 

String htmlResponse = "<html>"; 
htmlResponse += "<h2>Your city is: " + city +"<br/>"; 
htmlResponse += "</html>"; 

writer.println(htmlResponse); 
} 
} 
+1

請檢查瀏覽器控制檯是否有任何錯誤網絡選項卡是否發送請求。 – ranjeet8082

+0

我該怎麼做,我不確定這些事情是什麼意思:/ –

+0

如果您正在使用chrome,然後點擊三個垂直點圖標,用於打開設置。這個窗口將打開更多工具並選擇開發人員工具。一個新的窗口將打開。在此選擇控制檯選項卡中。這裏打印所有錯誤消息。還有網絡選項卡,顯示從瀏覽器發送的所有請求。 – ranjeet8082

回答

0

看到您的HTML代碼的代碼提交按鈕是不正確的,輸入標籤未關閉

代碼

<input id = "submit" type = "submit" value = "Submit" 

正確的應該是

<input id = "submit" type = "submit" value = "Submit"/>