简体   繁体   English

HTML不将数据发送到Java文件

[英]HTML not Sending Data to Java File

So I've had a bit of experience with front-end web dev and much experience with OOP (with Java), but I have never worked with processing user input from an HTML form and doing something with that data. 所以我在前端Web开发方面有一些经验,并且有很多使用OOP(使用Java)的经验,但我从来没有处理过从HTML表单处理用户输入并使用该数据做某事。 Since I've had so much experience with Java, I figured that would be the best language to work with. 由于我在Java方面有这么多的经验,我认为这将是最好的语言。 What I am trying to do is collect user data from a form (basic stuff: name, address, etc) and process it in a Java file. 我想要做的是从表单中收集用户数据(基本内容:名称,地址等)并在Java文件中处理它。 Right now, I don't think the data is even being sent over to the Java file, as when I click the "Submit" button, nothing happens. 现在,我不认为数据甚至被发送到Java文件,因为当我点击“提交”按钮时,没有任何反应。 What am I doing wrong here? 我在这做错了什么?

index.html: 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: 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);
}
}

seeing your html code your code for submit button is not correct, input tag is not closed 看到你的HTML代码你的代码提交按钮不正确,输入标签没有关闭

your code 你的代码

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

correct should be 应该是正确的

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM