Skip to content Skip to sidebar Skip to footer

How To Call User Input From Jsp File Into Java File

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:

Solution 1:

Use a servlet as your .java file,you can write your methods in this servlet class

publicclassMyServletextendsHttpServlet {
    privatestaticfinallongserialVersionUID=1L;


    protectedvoiddoGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        Stringname= request.getParameter("nameOne");
        System.out.println("<form action='Myservlet.do' method='get'>");
        System.out.println("nameOne is " + name);
    }


    protectedvoiddoPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
        Stringname= request.getParameter("nameThree");
        System.out.println("<form action='Myservlet.do' method='post'>");
        System.out.println("nameThree is " + name);
    }

}

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>

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">
        <inputtype="text" name="nameOne" value="Enter a Name" onClick="if(this.value == 'Enter a Name'){this.value = '';}" /> 
        <inputtype="text" name="nameTwo" value="Enter a Name" onClick="if(this.value == 'Enter a Name'){this.value = '';}" />
        <inputtype="submit">
    </form> 
    <form action="Myservlet.do" method="post">
        <inputtype="text" name="nameThree" value="Enter a Name" onClick="if(this.value == 'Enter a Name'){this.value = '';}" /> 
        <inputtype="submit">
    </form> 

Post a Comment for "How To Call User Input From Jsp File Into Java File"