src里面写java程序,web里面写前端,jsp这个单独另外算,
<%--
Created by IntelliJ IDEA.
User: master
Date: 2020-06-28
Time: 12:09
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$title>
head>
<body>
<h1>hello worldh1>
body>
html>
注意啊,这里就用到了映射这个概念,你直接输入http://localhost:8080/Demo02_war_exploded/
,其实就是直接访问out/artifacts/Demo02_war_exploded/
但是访问java不可以直接用这样子访问,要配置映射才可以,哪里配置映射?xml文件里面
<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">
<servlet>
<servlet-name>Myservletservlet-name>
<servlet-class>com.qiang.Demo01.Myservletservlet-class>
servlet>
<servlet-mapping>
<servlet-name>Myservletservlet-name>
<url-pattern>/myservleturl-pattern>
servlet-mapping>
web-app>
注意啊,这个xml文件配置了的,我们可以http://localhost:8080/Demo02_war_exploded/myservlet
访问java程序了,访问的东西是java里面的一个方法
java文件:
package com.qiang.Demo01;
import javax.servlet.*;
import java.io.IOException;
public class Myservlet implements Servlet {
@Override
public void init(ServletConfig servletConfig) throws ServletException {
}
@Override
public ServletConfig getServletConfig() {
return null;
}
@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
String id = servletRequest.getParameter("id");//传参数
System.out.println("服务端接受客户端的请求,参数为:"+id);//服务器给客户端的,在控制台显示
servletResponse.setContentType("text/html;charset=UTF-8");
servletResponse.getWriter().write("客户端接受到服务器端的回写");//服务器给客户端的,在网页端显示
}
@Override
public String getServletInfo() {
return null;
}
@Override
public void destroy() {
}
}
注解非常简单,一行就解决
@WebServlet("/demo02")
public class Myservlet2 implements Servlet {
//code........
}
这样的效果和上面的XML配置是一样效果的
init()进行初始化,service()进行业务处理,destroy()进行Servlet的销毁
package com.qiang.Demo01;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;
@WebServlet("/demo02")
public class Myservlet2 implements Servlet {
public void test(){
System.out.println("输出一句话");
}
@Override
public void init(ServletConfig servletConfig) throws ServletException {
System.out.println("Servlet初始化");
}
@Override
public ServletConfig getServletConfig() {
return null;
}
@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
System.out.println("Servlet进行业务");
}
@Override
public String getServletInfo() {
return null;
}
@Override
public void destroy() {
System.out.println("进行Servlet销毁");
}
}
多输入几次这个http://localhost:8080/Demo02_war_exploded/demo02
getServletName()
:返回servlet名称getInitParameter(String key)
:返回init参数的值(web.xml)getInitParameterNames()
:返回集合,包含所有的值getServletContext()
:返回ServletContext对象
<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">
<context-param>
<param-name>usernameparam-name>
<param-value>userparam-value>
context-param>
<servlet>
<servlet-name>Myservletservlet-name>
<servlet-class>com.qiang.Demo01.Myservletservlet-class>
<init-param>
<param-name>usernameparam-name>
<param-value>adminparam-value>
init-param>
<init-param>
<param-name>passwordparam-name>
<param-value>123456param-value>
init-param>
servlet>
<servlet-mapping>
<servlet-name>Myservletservlet-name>
<url-pattern>/myservleturl-pattern>
servlet-mapping>
web-app>
package com.qiang.Demo01;
import javax.servlet.*;
import java.io.IOException;
import java.util.Enumeration;
public class Myservlet implements Servlet {
@Override
public void init(ServletConfig servletConfig) throws ServletException {
System.out.println("返回类名"+servletConfig.getServletName());//返回类名
System.out.println("局部变量username:"+servletConfig.getInitParameter("username")+"\n");
Enumeration<String> initParameterNames = servletConfig.getInitParameterNames();
while (initParameterNames.hasMoreElements()){
System.out.println("迭代器中"+servletConfig.getInitParameter(initParameterNames.nextElement()));
}
ServletContext servletContext = servletConfig.getServletContext();
System.out.println("该项目名字"+servletContext.getContextPath());
System.out.println("服务器信息"+servletContext.getServerInfo());
System.out.println("全局变量的username:"+servletContext.getInitParameter("username"));
}
@Override
public ServletConfig getServletConfig() {
return null;
}
@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
}
@Override
public String getServletInfo() {
return null;
}
@Override
public void destroy() {
}
}
HttpServlet 继承 GenericServlet 继承 Servlet
上面我们的Servlet,只有一个service这个函数有用而已,其他的平常生活都用不到,但是又要写那么多,所以简化什么的,日常都是用HttpServlet的多
而且HttpServlet里面的东西方便快捷又多
package com.qiang.Demo01;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/test")
public class MyHTTPservlet extends HttpServlet {
//以get方式返回的时候调用函数
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().write("GET");
}
//已post方式返回的时候调用函数
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().write("POST");
}
}