invoking a servlet in java
How do i invoke a Servlet? and What 开发者_如何学运维is the difference in between doPost and doGet? any links for explanation are welcome
thank you
A servlet is typically invoked via a servlet mapping in your servlet container configuration, when a request is made to the servlet container for a path matching that mapping. There are a number of resources for learning more about servlets on the Sun Oracle Java site's servlet page. There's also an introductory article on Wikipedia. Edit: In the comments, @BalusC points out that StackOverflow's own page for the servlet
tag has quite a lot of useful info and links — nice one, Balus.
doPost
is called when the HTTP request is a POST
. doGet
is called when it's a GET
. There are other methods corresponding to the other HTTP verbs.
Invoking a servlet is done simply by navigating to the URL specified in your web.xml file in your web application. So if your servlet is called MyServlet you may have some code like this in your web.xml file:
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>com.mycompany.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/myservlet</url-pattern>
</servlet-mapping>
In this setup, navigating to http://myapplication.com/myservlet would invoke your servlet.
As far as the difference in doGet and doPost, the only difference is the HTTP method they respond to, as the servlet API abstracts any differences between actual HTTP GET and HTTP POST methods away from the programmer. This abstraction allows the programmer to get parameters from the request using a single interface and not have to bother with how the parameters were passed in. doGet is called when a HTTP GET request is sent to your servlet, typically by navigating to it directly. doPost is called when an HTTP POST request is sent to your servlet, which is commonly done with a form post from another html page.
About POST and GET: learn some HTTP basics
And some Servlet basics
精彩评论