Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting textbox values using JSP

I'm new in using JSP and I need to get a value from a textbox by upon clicking a button. I am using Java Netbeans with the server apache tomcat. This is how it works...

The textbox is enclosed in an HTML tag <table> once the user inputs a value, he clicks the button then a message box will appear with the value he entered.

I am not familiar with JSP and it gives me a hard time.

like image 791
Nina Avatar asked Feb 25 '23 02:02

Nina


2 Answers

You need to configure a servlet in web.xml

Create a form and post the date to that servlet

Here I have outlined how it should look

Your JSP

<form action"/yourServlet" method ="post">
<input type="text" name="age"/>
<input type="SUBMIT" />
</form>

Your Servlet

doPost(....){
  String age = request.getParameter("age");
}

Must See

  • Servlet wiki page
like image 193
jmj Avatar answered Mar 05 '23 19:03

jmj


My answer is quite familiar with the first answer.

mainPage.jsp

<html>
  <head>
    <title>Your Title Here</title>
  </head>
  <body>
    <form action="sample.jsp" method="POST">
      <input type="text" id="firstname" name="firstname" />
      <input type="submit" value="Submit" />
    </form>
  </body>
</html>

sample.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%
    String firstname = request.getParameter("firstname");
    /* 
     * Some code here
     */
%>

I'm also using apache tomcat.

You have to configure your web.xml as said in the first answer.

Hope that helps.

like image 41
sailhenz Avatar answered Mar 05 '23 20:03

sailhenz