java 从html文件获取文本字段字符串输入以在java类中处理/使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17291849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 01:36:13  来源:igfitidea点击:

Get textfield string input from html file to process/use in java class

javahtmlservlets

提问by Suzuka

Okay, I have no idea about what I am doing exactly but I am trying to understand it through sample programs and snippets. To be clear, my concepts are missing. So I might be having an error which is very obvious (not to me).

好的,我不知道我在做什么,但我试图通过示例程序和片段来理解它。需要明确的是,我的概念缺失了。所以我可能有一个非常明显的错误(对我来说不是)。

I am creating a simple username-password field on a localhost and need to be able to use the textfield inputs to match them. My page shows the buttons and fields properly(index.htm in WEB-INF) but my java class needs to get the string values. I get it that form actions need URL but how can I send it to my java clasS? I tried using request.getParamater("id") but it says no attached doc.

我正在本地主机上创建一个简单的用户名密码字段,并且需要能够使用文本字段输入来匹配它们。我的页面正确显示了按钮和字段(WEB-INF 中的 index.htm),但我的 java 类需要获取字符串值。我知道表单操作需要 URL,但如何将其发送到我的 java 类?我尝试使用 request.getParamater("id") 但它说没有附加文档。

below is the html part

下面是html部分

<!DOCTYPE html>
<html>

<head><title> Multi Factor Authentication Prototype </title>
<link rel="stylesheet"
  href="./css/styles.css"
  type="text/css"/>
</head>


<body>
<table class="title">
  <tr><th>Multi-factor Authentication, level 1 prototype </th></tr>
</table>

<p/>
<fieldset>
<form name="mfa1" action="check" method="post">
<legend> Login </legend> <ul> <br />
Username : <input title="Please Enter the Username" id="uname" name="uname" type="text" size="20" /><br />
<br />
Password : <input title="Please Enter your password" id="pwd" name="pwd" type="password" size="20" /><br />
<br />

</body>
</html>

here is java class. It says no java doc referred in request field and yes, doesn't display anything.

这是java类。它说请求字段中没有引用 Java 文档,是的,不显示任何内容。

package test;

import java.io.*;

import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.*;

import java.lang.*;

@WebServlet("/check")

public class TestServlet extends HttpServlet{


@Override
  public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
      throws ServletException, IOException {
    response.setContentType("text/html");
    String un = request.getParameter("uname");
    PrintWriter out = response.getWriter();
    out.println
      ("<!DOCTYPE html>\n" +
       "<html>\n" +
       "<head><title>tuiy</title></head>\n" +
       "<body bgcolor=\"#fdf5e6\">\n" +
       "<h1>"+un+"</h1>\n" +
       "\n" +
       "</body></html>");


}
}

or is there a way to insert if/else clauses and various functions in the same dcument? I would really prefer if you could help me point out the way to get the parameters in java.

或者有没有办法在同一个文件中插入 if/else 子句和各种函数?如果你能帮我指出在java中获取参数的方法,我真的更喜欢。

回答by Jan Thom?

You are using a doGet method, but your form is using POST. Use a doPost method. See also doGet and doPost in Servlets

您使用的是 doGet 方法,但您的表单使用的是 POST。使用 doPost 方法。另请参阅Servlet 中的 doGet 和 doPost