所以我在前端Web开发方面有一些经验,并且在使用OOP(使用Java)方面有很多经验,但我从未处理过从HTML表单处理用户输入并使用该数据执行某些操作。由于我在Java方面有这么多的经验,我认为这将是最好的语言。我想要做的是从表单中收集用户数据(基本内容:名称,地址等)并在Java文件中处理它。现在,我不认为数据甚至被发送到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);
}
}
答案 0 :(得分:0)
查看您的html代码,您的提交按钮代码不正确,输入标记未关闭
你的代码
<input id = "submit" type = "submit" value = "Submit"
正确应该是
<input id = "submit" type = "submit" value = "Submit"/>