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

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

}

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

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