2016-09-27 75 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"/>