views:

322

answers:

1

I created one web application project. It contains a servlet file and a HTML file. How do I call servlet file from HTML?

+1  A: 

Just map the servlet on a certain url-pattern in web.xml and then let the HTML link or form action point to an URL which matches the url-pattern of the servlet.

E.g.

<servlet>
    <servlet-name>login</servlet-name>
    <servlet-class>com.example.LoginServlet</servlet-name>
</servlet>
<servlet-mapping>
    <servlet-name>login</servlet-name>
    <url-pattern>/login</url-pattern>
</servlet-mapping>

in combination with

<a href="login">Login</a>

and/or

<form action="login" method="post">
    <input type="text" name="username">
    <input type="password" name="password">
    <input type="submit">
</form>

in HTML (or JSP).

Links and forms with method="get" will invoke doGet() method of the servlet and forms with method="post" will invoke doPost() method of the servlet.

To learn more about servlets, I strongly recommend to go through the basic tutorials which you can find at Coreservlets.com and the Sun Java EE 5 tutorial part II chapter 4. If you prefer books, I can then recommend the Head First Servlets & JSP.

BalusC
thanks,but it shows another error.(i.e)Error 500
An exception was been thrown. Read the stacktrace/logs and fix code accordingly.
BalusC