从servlet获取信息创建了html

时间:2011-03-12 03:36:45

标签: java html servlets

我有一个servlet,它创建一个html文本框,然后在提交时重定向到另一个servlet。如何从新的servlet访问html文本框的值?我能够从新的servlet访问servlet变量,但我不知道如何访问html生成的代码的值。

感谢,

这是获取文本输入的servlet

  public class ServletB extends HttpServlet {



    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
    {

        response.setContentType("text/html");

String value = System.getProperty("card");


PrintWriter out = response.getWriter();


out.println("<center><h1>Your preffered method of payment is "+value+"</h1><br />");
out.println("Please Enter Card Number<input type =\"text\" name = \"number\"/><form action=\"http://codd.cs.gsu.edu:9999/cpereyra183/servlet/ServletC\"><input type =\"submit\" value=\"Continue\" /><input type=\"button\" value=\"Cancel\" /></center>");

    }
   }}

这是servlet第一个servlet重定向到我所做的就是尝试输出文本输入

  public class ServletC extends HttpServlet {



    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException
    {

        response.setContentType("text/html");

String value = System.getProperty("card");


PrintWriter out = response.getWriter();

out.println(request.getParameter("number")); 
    }
    }

3 个答案:

答案 0 :(得分:2)

如果您为输入字段指定名称

<input type="text" name="foo">

然后您可以通过输入字段的名称在后处理servlet中将其作为请求参数进行访问。

String foo = request.getParameter("foo");

另见:


与具体问题无关,与大多数servlet教程想要让我们相信的相反,HTML实际上属于JSP,而不是Servlet。我建议把这个HTML放在JSP中。

答案 1 :(得分:1)

如果您的标记看起来像这样......

<form action="anotherServlet">
    <input name="myTextbox" />
</form>

...然后您可以使用doGet()或[{1}} doPost()方法中的HttpServletRequest对象获取值,如下所示:

anotherServlet

请参阅:ServletRequest#getParameter()

答案 2 :(得分:1)

public class Formvalid extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    PrintWriter pr = response.getWriter();
    boolean flag = true;
int count=0;
    response.setContentType("text/html");
    Enumeration enume;
    enume = request.getParameterNames();
    while (enume.hasMoreElements()) {
        count++;
        String name = (String) enume.nextElement();
        String value = request.getParameter(name);
        if (value == null ||  value.equals("")) {

            pr.println("<h5 style='color:red;'>please enter manditory values  :"
                    + name + "</h5>");
            flag = false;
        } 

    }

    pr.println("<h3>Employe Registation</h3>");

    if (!flag || count==0) {
        pr.println("<form method=\"get\" action=\"formvalid\"><br />EmpName *:<input type='text' name='Empname' ><br />"
                + "Age *:<input type='text' name='age' ><br /><tr><td>Qulification *:<input type='text' name='Qualification' ><br />Address<textarea> </textarea><br /><input type='submit' value='submit'><input type='reset' value='reset'></FORM>");
    } else {
        pr.println("<h3 style='color:green;'>submitted successfully</h3>");
    }

}

}