简体   繁体   中英

How to pass text field value from jsp to java class

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

my .jsp code is

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

my .java class code is

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

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

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

Call servet from form tag and then get value using request.getParameter("your value") api of request object.

JSP Page:

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

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

A.jsp file

<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

<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

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 

   }

}

Is your java class a servlet?

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

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

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

name.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 action and method (optional, by default it takes "get") attributes.

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

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 will be as follows,

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

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:

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;

}

 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM