Servletå¦ä¹ ç¬è®°ï¼ä¸ï¼
程序员文章站
2022-06-21 22:33:12
...
Servletæ¦è¿°
Jspçå身就æ¯Servletã
Servletæ¯å¨æå¡å¨ä¸è¿è¡çå°ç¨åºãä¸ä¸ªServletå°±æ¯ä¸ä¸ªJavaç±»ï¼å¹¶ä¸å¯ä»¥éè¿â请æ±-ååºâç¼ç¨æ¨¡å¼æ¥è®¿é®çè¿ä¸ªé©»çå¨æå¡å¨å
åéçServletç¨åºã
Tomcat容å¨ç级
å个ç级ã Containnerï¼Engineï¼HOSTï¼Servletï¼Contextï¼ä¸ä¸æï¼
Servlet容å¨ç®¡çContext容å¨ï¼ä¸ä¸ªContext对åºä¸ä¸ªWebå·¥ç¨
ç¼åä¸ä¸ªServlet
- å建ä¸ä¸ªèªå·±çServletç±»ï¼ç»§æ¿HttpServlet
- éådoGetï¼ï¼æè doPostï¼ï¼æ¹æ³ãå ·ä½éååªä¸ªåå³å»æ交请æ±çæ¹å¼ã
- å¨web.xmlä¸æ³¨åServlet
主è¦æ¯ä»¥ä¸ä¸¤ä¸ªæ ç¾
<servlet-name>HelloServlet</servlet-name>
<servlet-class>servlet.HelloServlet</servlet-class>
- éè¿web页é¢å°±å¯ä»¥è¿è¡Servletç访é®
举ä¾ï¼
//HelloServlet.java
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("å¤çget请æ±");//æå°å°æ§å¶å°ï¼
resp.setCharacterEncoding("utf-8");//注æç¼ç é®é¢ï¼è½¬æ¢æUTF-8ï¼å¦åä¼åºç°ä¹±ç
resp.setContentType("text/html;charset=UTF-8");
PrintWriter out = resp.getWriter();
out.println("<strong>欢è¿å
临</strong><br>");//è¾åºå°web页é¢
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//doPostä¹ä¸æ ·ï¼å°±ä¸éå¤åäº
super.doPost(req, resp);
}
}
<--web.xml--!>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>ServletDemo</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>servlet.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/servlet/HelloServlet</url-pattern>
</servlet-mapping>
</web-app>
<--index.html--!>
<%@ page language="java" contentType="text/html; charset=utf-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>my first ServletÂ</h1>
<hr>
<a href="servlet/HelloServlet">get()请æ±âHelloServlet</a>
</body>
</html>
注æäºé¡¹ï¼
- è®°å¾è½¬æ¢ç¼ç 为UTF-8
- xmlæ件ä¸ç servlet-class å url-patternå¿ é¡»ä¸indexä¸çé¾æ¥å°åä¸æ ·ï¼æåservletç±»çå ¨å°åï¼src为项ç®æ ¹ç®å½.
上一篇: ln -s 软连接介绍
下一篇: 女人更年期脾气暴躁怎么调理最好
推荐阅读
-
Struts2访问servlet分享
-
SpringBoot 源码解析 (六)----- Spring Boot的核心能力 - 内置Servlet容器源码分析(Tomcat)
-
Java:Servlet
-
Servlet | Request 对象获取请求参数的通用方式
-
jsp和servlet操作mysql中文乱码问题的解决办法
-
JSP 开发之Servlet解决网页缓存问题
-
解决cannot be cast to javax.servlet.Filter 报错的问题
-
jsp传参 servlet接收中文乱码问题的解决方法
-
Servlet实现文件上传的三种方法总结
-
JSP+Servlet制作Java Web登录功能的全流程解析