0

I am new to servlets so very simple answer will be much appreciated. My problem is I have written a code of servlet extracting the values from HTML form. But when I run my HTML form like

http://localhost:8080/secondTry/form.html

html form appears okay. When I enter firstname and lastname, and press the submit button it is not showing any output.. I don't know why..I have searched it on internet but all in vain. Moreover, is it has something to do with my tomcat?

Below is my file form.html:

<html>
<body>
<form action="/formServlet" method="get">
First name: <input type="text" name="fname"><br>
Last name: <input type="text" name="lname"><br>
<input type="button" value="submit">
</form>
</body>
</html>

secTry.java:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class secTry extends HttpServlet{
    public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException,ServletException{
        String fname = req.getParameter("fname");
        String lname = req.getParameter("lname");
        PrintWriter out = res.getWriter();
        out.println("Hello "+ fname + " " +lname + "...!!!");
    }
}

web.xml file:

<web-app>
  <servlet>
    <servlet-name>secTry</servlet-name>
    <servlet-class>secTry</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>secTry</servlet-name>
    <url-pattern>/formServlet</url-pattern>
  </servlet-mapping>
</web-app>

PS: I'm compiling my java file on command line using

javac -cp .;C:/apache-tomcat-8.5.23/lib/servlet-api.jar secTry.java

Please help me and tell me where I am doing wrong?

sheplu
  • 2,937
  • 3
  • 24
  • 21

1 Answers1

1

When I enter firstname and lastname, and press the submit button it is not showing any output.

Based on the code you have provided you are not submitting the form at all:

<input type="button" value="submit">

This is not a submit element. Form will be submitted when element type is submit:

<input type="submit" value="submit">
Aleh Maksimovich
  • 2,622
  • 8
  • 19