I. httpServlet
1. guide pack
//Find the package by searching Maven servlet (class name) to pom.xml import package
<dependencies> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.15</version> </dependency> <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency> <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.47</version> </dependency> </dependencies>
2. Create a Servlet
2.1 no ginseng
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub ProductService ps=new ProductService(); ArrayList ls=ps.selectAll(); String rs=JSONObject.toJSONString(ls);//Convert an object to a string in json format response.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); response.getWriter().append(rs); }
Converting an object to a string in json format: Chinese recognition
response.getWriter().append("hello Java10 ");.append(request.getContextPath());
//Chinese is not recognized by default
String rs=JSONObject.toJSONString(ls); //Tell the servlet to transcode with UTF-8 response.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8");//html
There are two types of response return: byte stream outputstream and character stream printwrite.
- Character stream, to output Chinese, you need to set response.setCharacterEncoding("UTF-8");
Details: https://blog.csdn.net/tlms_/article/details/78749980 -
The function of response.setContentType(MIME) is to make client browser distinguish different kinds of data, and call different program embedded modules in browser to process corresponding data according to different mime.
(web browser is to judge whether the file is a GIF image by MIME type. Handle json strings through MIME types)
Details: https://blog.csdn.net/qq_42108192/article/details/81938674
Effect
//http://localhost:8080/homework/UserServlet
2.2 have ginseng
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub //Take out the parameters in the request String productname=request.getParameter("pname"); ProductService ps=new ProductService(); Product p1=ps.findByName(productname); //Convert an object to a string in json format String rs=JSONObject.toJSONString(p1); //Solve the problem of Chinese code disorder response.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); response.getWriter().append(rs); }
Take out the parameters in the request
String productname=request.getParameter("pname");
Effect
//http://localhost:8080/homework/StudentService?pname = banana
II. java inheritance
Parent class
public class Parent { public String name; public int id; public int add(int a,int b) { return a+b; } }
Subclass
public class Child extends Parent{ public int age; public int delive(int a, int b) { return a-b; } }
extends means inheritance
-
A extends B means that a inherits B, that is, a is a subclass and B is a parent.
-
Subclass has properties and methods of the parent
-
Both parents and children have a method with the same name, parameter and return type.
//This is called override.
public class Child extends Parent{ public int add(int a,int b) { return a+b+1; } }
public class Test { public static void main(String[] args) { // TODO Auto-generated method stub Child c=new Child(); Parent p=new Parent(); c.age=10; c.id=1;//Parent attribute c.name="Little flower";//Parent attribute //c.add(1, 2); / / subclass inherits method of parent class p.id=2; p.name="Little flower father"; System.out.println("The child class inherits the properties of the parent class------"+c.id+c.name); System.out.println("Subcategory addition:"+c.add(3, 4));//--8 System.out.println("Parent class addition:"+p.add(3, 4));//--7 }
polymorphic
//The type of the declared variable is the parent class //The construction method uses the construction method of subclass Parent p2=new Child(); System.out.println("Polymorphic addition:"+p2.add(3, 4));
Note: the object created is a subclass (see constructor), so the output is: 8