如何将文本字段值从jsp传递给java类。
我的 .jsp 代码是
<html>
<head></head>
<body>
<FORM>
Please enter your name:
<INPUT TYPE="TEXT" NAME="text1">
<BR>
<INPUT TYPE="SUBMIT" value="Submit">
</FORM>
</body>
</html>
我的 .java 类代码是
这里的字符串str我需要获取文本字段值。
class sample{
String str=""; //C:/check/svntes
File exportDir = new File(str);
if (exportDir.exists()) {
SVNErrorMessage err = SVNErrorMessage.create(SVNErrorCode.IO_ERROR, "Path ''{0}'' already exists", exportDir);
throw new SVNException(err);
}
exportDir.mkdirs();
}
答案 0 :(得分:3)
要将值从JSP传递到Java,您需要java Servlet
。
从form
代码调用servet,然后使用request.getParameter("your value")
对象的request
api获取值。
JSP页面:
<form action="HelloServlet" method="POST">
Please enter your name:
<input type="text" name="text1" id="text1">
<br>
<input type="submit" value="Submit">
</form>
Servlet:
public class HelloWorld extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// reading the user input
String text1= request.getParameter("text1");
}
}
答案 1 :(得分:3)
<html>
<head></head>
<body>
<FORM ACTION="B.JSP" METHOD="POST"> //edited part
Please enter your name:
<INPUT TYPE="TEXT" NAME="text1">
<BR>
<INPUT TYPE="SUBMIT" value="Submit">
</FORM>
</body>
</html>
<jsp:useBean id="sample" scope="page" class="com.home.file.sample" /> // sample is java file name
String name = request.getParameter("text1");
int iRowAffected = 0;
//-------now pass parameter "name" to your sample java file
sample.function_name("name");
public class sample
{
public int function_name(String NAME)
{
String str = NAME;
File exportDir = new File(str);
if (exportDir.exists()) {
SVNErrorMessage err = SVNErrorMessage.create(SVNErrorCode.IO_ERROR, "Path ''{0}'' already exists", exportDir);
throw new SVNException(err);
}
exportDir.mkdirs();
//continue with your coding
}
}
答案 2 :(得分:1)
您的java类是servlet吗?
因为那样你需要像这样发布到你的servlet:
<form action="ServletName" method="GET">
Please enter your name:
<input type="text" name="text1" />
<br />
<input type="submit" value="Submit" />
</form>
然后在你的servlet中你可以得到这样的字符串值:
String str = request.getParameter("name");
答案 3 :(得分:1)
<强> name.jsp 强>
<FORM action="/submitName" method="get">
Please enter your name:
<INPUT TYPE="TEXT" NAME="text1">
<BR>
<INPUT TYPE="SUBMIT" value="Submit">
</FORM>
首先,在上面的jsp文件中,有两件事缺少动作 和方法(可选,默认情况下需要&#34; get&#34;)属性。
现在要获取java类中的输入值,您需要编写 Servlet 类并使用URL映射在 web.xml 中对其进行配置&#34; / submitName&#34; 强>
<强> MyServlet.java 强>
// Import required java libraries
// Extend HttpServlet class
public class MyServlet extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
String name = request.getParameter("text1"); //should be same name as form input name
System.out.println(name);
}
}
web.xml 将如下,
<web-app>
<servlet>
<servlet-name>myservlet</servlet-name>
<servlet-class>MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myservlet</servlet-name>
<url-pattern>/submitName</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>name.jsp</welcome-file>
</welcome-file-list>
</web-app>
答案 4 :(得分:1)
我通过这种方式得到答案。它工作正常。
my.jsp code:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form >
Enter the word: <input type="text" name="word">
<input type="submit">
<%@ page import = "dem.hashmap"%> <!-- //importing java class -->
<%
hashmap hm = new hashmap(); /* creating reference for my java class */
String inputvalue = request.getParameter("word");
String output = hm.dircreation(inputvalue); /* calling java method */
out.println(inputvalue);
%>
</body>
</html>
我的hashmap .java类:
package dem;
import java.io.File;
public class hashmap {
String nav;
public String dircreation(String dir)
{
System.out.println("The Value is--------->"+dir);
boolean success = false;
File directory = new File(dir);
System.out.println("1....The Value is--------->"+dir);
if (directory.exists()) {
System.out.println("Directory already exists ...");
} else {
System.out.println("Directory not exists, creating now");
success = directory.mkdir();
if (success) {
System.out.printf("Successfully created new directory : %s%n", dir);
} else {
System.out.printf("Failed to create new directory: %s%n", dir);
}
}
return nav;
}
}