如何将文本字段值从jsp传递到java类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28337199/
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-08-11 06:02:47  来源:igfitidea点击:

How to pass text field value from jsp to java class

javahtmljsp

提问by kumaresh

How to pass text field value from jsp to java class.

如何将文本字段值从 jsp 传递到 java 类。

my .jspcode is

我的.jsp代码是

<html>
<head></head>
<body>
    <FORM>
        Please enter your name:
        <INPUT TYPE="TEXT" NAME="text1">
        <BR>
        <INPUT TYPE="SUBMIT" value="Submit">
    </FORM>
</body>
</html>

my .javaclass code is

我的.java类代码是

here in string str i need to get the textfield value.

在字符串 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();
}

采纳答案by user3835327

Hmm .. let's assume how your jsp & java file interact with each other. Correct if im wrong.

嗯.. 让我们假设您的 jsp 和 java 文件如何相互交互。如果我错了,请纠正。

A.jsp file

a.jsp 文件

<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>

B.JSP

B.JSP

<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");

Sample.java

示例.java

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 

   }

}

回答by atish shimpi

To passing value from JSP to Java, you need java Servlet.

要将值从 JSP 传递到 Java,您需要 java Servlet

Call servet from formtag and then get value using request.getParameter("your value")api of requestobject.

form标签调用servet ,然后使用对象的request.getParameter("your value")api获取值request

JSP Page:

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"); 
 }
}

回答by s3t

Is your java class a servlet?

你的java类是servlet吗?

Because then you need to post to your servlet like this:

因为那时你需要像这样发布到你的servlet:

<form action="ServletName" method="GET">
    Please enter your name: 
    <input type="text" name="text1" />
    <br />
    <input type="submit" value="Submit" />
</form>

And then in your servlet you can get the string value like this:

然后在你的 servlet 中你可以得到这样的字符串值:

String str = request.getParameter("name");

回答by Sandeep Patange

name.jsp

名称.jsp

<FORM  action="/submitName" method="get">
        Please enter your name:
        <INPUT TYPE="TEXT" NAME="text1">
        <BR>
        <INPUT TYPE="SUBMIT" value="Submit">
    </FORM>

First of all, in your above jsp file two things are missing actionand method(optional, by default it takes "get") attributes.

首先,在上面的 jsp 文件中,缺少actionmethod(可选,默认情况下它采用“get”)属性的两件事。

Now to get the input value in you java class, you need to write a Servletclass and configure it in the web.xmlwith a url mapping "/submitName".

现在要获取 java 类中的输入值,您需要编写一个Servlet类并在web.xml 中使用 url 映射"/submitName" 对其进行配置

MyServlet.java

我的Servlet.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.xmlwill be as follows,

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>

回答by kumaresh

I got answer by this way..Its working fine.

我通过这种方式得到了答案..它工作正常。

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>

my hashmap .java class:

我的 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;

}

 }