This lesson has some basic servlet knowledge!
1. Access the servlet locally (take the tomcat server as an example ):
Http: // locatlhost: 8080/servlet/name of your servlet
In this way, you can directly access this servlet!
2. When using jcreator for servlet programming, you need to import common/lib/servlet. jar under tomcat.
To call the servlet package
3. Test the program from the simplest Hello, World! To practice!
Code:
Import javax. servlet .*;
Import java. io .*;
Public class HelloServlet extends GenericServlet
{
// The service of the initial image class needs to be reloaded.
Public void service (ServletRequest request, ServletResponse response) throws IOExcetpion
{
PrintWriter out = response. getWriter ();
Out. println ("Hello, this is our world! ");
}
}
4. HttpServlet inherits GenericServlet, HttpServletRequest/HttpServletResponse inherits ServletRequest/ServletResponse
The getHeader method is used to obtain the header information.
Request. getHeader ("connection") to obtain the connection information
Request. getHeader ("User-Agent") to obtain User information
Example:
Import javax. servlet .*;
Import java. io .*;
Import javax. servlet. http. *; // you must import this package to use HttpServlet
Public class HelloServlet extends HttpServlet
{
// The service of the initial image class needs to be reloaded.
Public void service (HttpServletRequest request, HttpServletResponse response) throws IOExcetpion
{
PrintWriter out = response. getWriter ();
String s = request. getHeader ("Connection ");
String user = request. getHeader ("User-Agent ");
Out. println ("you connect to server is" + s );
Out. close ();
}
}
5. response. setContentType ("text/html; charset = gb2312 ")
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service