Java servlet

dawn 2022-06-24 07:57:24 阅读数:435

javaservlet

Conventional JSP Pages are now used less ,servlet Apply more .

Servlet yes Server Applet For short , Is a server-side program , Interact with the client , Process the client's request and complete the response , It belongs to dynamic web page technology ,JavaEE Part of the code .

1、 Build development environment

take Servlet relevant jar package ( lib\servlet-api.jar ) Configuration to classpath in .

2、 To write serlet

⑴  Realization javax.servlet.Servlet;

⑵ rewrite 5 Main methods ;

⑶ stay service() Method and return it to the client .

The basic format is fixed , Write in imitation of :

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.ServletException;
import java.io.IOException;
public class ServletFirst implements Servlet{
public void init(ServletConfig config) throws ServletException{
}
public void service(ServletRequest request,ServletResponse response) throws ServletException,IOException{
System.out.println("ServletDemo");
String name=request.getParameter("name");
System.out.println(name);
String age=request.getParameter("age");
System.out.println(age);
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
response.getWriter().write(" full name :"+name+" Age :"+age);
}
public void destroy(){
}
public ServletConfig getServletConfig(){
return null;
}
public String getServletInfo(){
return null;
}
}

3、 stay tomcat Preparatory work in

stay tomcat Of webapps Create a new directory JavaWeb2022, The structure under this directory :

webapps( Catalog )
---JavaWeb2022( Catalog )
---WEB-INF( Catalog )
---classes( Catalog )
---lib

Will be compiled class File copy to classes Next .

stay WEB-INF New under the directory web-xml file , Content :

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0"
metadata-complete="true">
<servlet>
<servlet-name>MyServlet1</servlet-name>
<servlet-class>ServletFirst</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet1</servlet-name>
<url-pattern>/servletdemo</url-pattern>
</servlet-mapping>
</web-app>

among /servletdemo Is the access path ,/servletdemo=》MyServlet1=》ServletFirst, This corresponds to .

4、 Prepare front-end files

Create a new one index.html file :

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>123</title>
<script src="jquery-3.4.1.min.js"></script>
</head>
<body>
<button onclick="GetData()"> get data </button>
<div id="demo"></div>
<script>
function GetData(){
$.ajax({
url: 'http://127.0.0.1:9119/JavaWeb2022/servletdemo',
dataType: "text",
async: true,
data:{
name:"QWE",
age:13
},
type: "POST",
beforeSend:function(){},
success: function(data) {
console.log(data);
document.getElementById("demo").innerHTML = document.getElementById("demo").innerHTML + data+"<br>";
}
});
}
</script>
</body>
</html>

5、 Access in web pages

  start-up tomcat, visit http://127.0.0.1:9119/JavaWeb2022/index.html

The result is from servlet Back to the front page .

copyright:author[dawn],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/175/202206240359137325.html