Baidu Encyclopedia says:
Servlet (Server Applet) is the abbreviation of Java Servlet, called a mini-service program or service connector. It is a server-side program written in Java. Its main function is to interactively browse and modify data and generate dynamic Web content.
Common ways:
It is a small Java program running on the server side, accepting and responding to requests sent from the client
effect:
Process client requests and respond to requests
Write a serclet step
1. Write a class
Inherited from HttpServlet
Rewrite doGet and doPost methods
2. Write configuration files (web.xml)
Register first and then bind
3. Visit
http://localhost/project name/path
Notice:
Receive parameters: Format: value=key
String value = request.getParameter("key");
For example: http://localhost/day09/hello?username=tom
In, String value = request.getParameter("username");
Write back parameters:
response.getWriter().print("success");
Handle garbled problems in response:
resp.setContentType("text/html;charset=utf-8"); is usually placed in the first line
The following is the original code:
public class RequestServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html;charset=utf-8"); // Receive parameter String value = req.getParameter("username"); System.out.println(value); // Write data back to the browser resp.getWriter().print("data:"+value); resp.getWriter().print("Hello"); }} web.xml configuration
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <!-- Using servlet tag--> <servlet> <servlet-name>HelloServlet</servlet-name> <servlet-class>cn.itcast.a_hello.HelloServlet</servlet-class> </servlet> <servlet> <servlet-name>RequestServlet</servlet-name> <servlet-class>cn.itcast.b_request.RequestServlet</servlet-class> </servlet> <!-- Bind Path--> <servlet-mapping> <servlet-name>HelloServlet</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>RequestServlet</servlet-name> <url-pattern>/request</url-pattern> </servlet-mapping></web-app>
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.