如何将用户输入从JSP文件调用到Java文件 [英] How to call user input from JSP file into Java file
问题描述
我正在使用JSP编写程序.我有一个包含一些方法的.java文件,并且我有一个.jsp文件,除了一些javascript方法之外,该文件还包含以下内容:
I'm writing a program using JSP. I have a .java file containing a few methods, and I have a .jsp file which contains the following, in addition to a few javascript methods:
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<div id="nameDiv"></div>
<div id="nameInput">
<input type="text" name="nameOne" value="Enter a Name" onClick="if(this.value == 'Enter a Name'){this.value = '';}" />
<input type="text" name="nameTwo" value="Enter a Name" onClick="if(this.value == 'Enter a Name'){this.value = '';}" />
<input type="text" name="nameThree" value="Enter a Name" onClick="if(this.value == 'Enter a Name'){this.value = '';}" />
<input type="button" name="btnUpdate" value="Update Name" />
</div>
我的问题是:如何在Java文件中调用来自这些文本框的用户输入?更好的说是,如何将这些在单独文件(JSP)中的输入调用到我的另一个单独的Java文件中,并使用用户在文本框中输入的值?
My question is: how can I call the user input from these textboxes, in a java file? Or better said, how can I call these inputs which are on a separate file (JSP), into my other separate java file and use the value that the user input in the textbox?
推荐答案
使用servlet作为.java文件,您可以在该servlet类中编写方法
Use a servlet as your .java file,you can write your methods in this servlet class
public class MyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("nameOne");
System.out.println("<form action='Myservlet.do' method='get'>");
System.out.println("nameOne is " + name);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("nameThree");
System.out.println("<form action='Myservlet.do' method='post'>");
System.out.println("nameThree is " + name);
}
}
并将其添加到WebContent/WEB-INF中的web.xml中:
and add this to your web.xml in WebContent/WEB-INF :
<servlet>
<!--whatever-->
<servlet-name>MyServlet</servlet-name>
<!--the position of your own servlet-->
<servlet-class>com.stackoverflowquizz.servlet.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<!--the same as the one in <servlet>-->
<servlet-name>MyServlet</servlet-name>
<!--the action or the url that you can access this servlet-->
<url-pattern>/Myservlet.do</url-pattern>
</servlet-mapping>
在.jsp文件中使用<form action = "xx" method="get/post"> <input type="submit">
将args传递给.java(servlet文件)
use <form action = "xx" method="get/post"> <input type="submit">
in .jsp file to pass args to .java (servlet file)
<form action="Myservlet.do" method="get">
<input type="text" name="nameOne" value="Enter a Name" onClick="if(this.value == 'Enter a Name'){this.value = '';}" />
<input type="text" name="nameTwo" value="Enter a Name" onClick="if(this.value == 'Enter a Name'){this.value = '';}" />
<input type="submit">
</form>
<form action="Myservlet.do" method="post">
<input type="text" name="nameThree" value="Enter a Name" onClick="if(this.value == 'Enter a Name'){this.value = '';}" />
<input type="submit">
</form>
这篇关于如何将用户输入从JSP文件调用到Java文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!