Servlet

加载jar包

在这里插入图片描述

配置tomcat

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

WEB_INF

<?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">

    <servlet>
        <servlet-name>a1</servlet-name>
        <servlet-class>
            web.TestServlet
        </servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>a1</servlet-name>
        <url-pattern>/test</url-pattern>
    </servlet-mapping>

    <!--全局初始化参数=全局变量-->
    <context-param>
        <param-name>allparam</param-name>
        <param-value>allparam参数</param-value>
    </context-param>

    <servlet>
        <servlet-name>b1</servlet-name>
        <servlet-class>
            web.UsersServlet
        </servlet-class>
        <init-param>
            <!--针对某一个servle=局部变量-->
            <param-name>myparam</param-name>
            <param-value>myparam的值</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>b1</servlet-name>
        <url-pattern>/testrequest</url-pattern>
    </servlet-mapping>
</web-app>

web.my.html

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <h1>我来啦</h1>
    <a href="test">test-first-servlet</a>
    <form action="test" method="post">
        <input type="submit" value="post-test">
    </form> <hr>
    <h1>测试request</h1>
    <a href="testrequest?upass=abc&uage=12">test-requedt-get</a>
    <form action="testrequest" method="post">
        uname:<input type="text" name="username">
        爱好:<input type="checkbox" name="aihao" value="lanqiu">篮球
        <input type="checkbox" name="aihao" value="paiqiu">排球
        <input type="checkbox" name="aihao" value="zuqiu">足球

        <input type="submit" value="提交">
    </form>
</body>
</html>

web.success.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
  <h1>success.html</h1>
</body>
</html>

src.web.StudentServlet.java

package web;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//name 可有可无 urlPatterns{"","",""}
@WebServlet(urlPatterns = "/stu/test",name = "myname",initParams = {
        @WebInitParam(name="init-stu",value = "myinitparam")
},loadOnStartup = 1)
public class StudentServlet extends HttpServlet {
    @Override
    public void init(ServletConfig config) throws ServletException {
        System.out.println("StudentServlet-init"+config.getInitParameter("init-stu"));
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("do-get-StudentServlet");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}

src.web.TestServlet.java

package web;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class TestServlet extends HttpServlet {

    @Override
    public void init(ServletConfig config) throws ServletException {
        //局部初始化参数
        System.out.println("TestServlet-init->myparam"+config.getInitParameter("myparam"));
        //全局初始化参数
        System.out.println("TestServlet-init->allparam"+config.getInitParameter("allparam"));
    }

    public TestServlet() {
        System.out.println("TestServlet");
    }

    @Override
    public void destroy() {
        System.out.println("servlet销毁");
    }

    @Override
    public void init() throws ServletException {
        System.out.println("servlet初始化");
    }

    /*@Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.service(req, resp);
    }*/

    @Override   //处理get请求的 HttpServletRequest 请求对象 HttpServletResponse 响应对象
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("get-------");
        ServletContext servletContext = req.getServletContext(); //全局对象
    }

    @Override   //处理post请求
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("post-------");
    }
}

src.web.UsersServlet.java

package web;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

public class UsersServlet extends HttpServlet {

    @Override
    public void init(ServletConfig config) throws ServletException {
        //局部初始化参数
        System.out.println("UsersServlet-init->myparam"+config.getInitParameter("myparam"));
        //全局初始化参数
        System.out.println("UsersServlet-init->allparam"+config.getInitParameter("allparam"));
    }

    public UsersServlet() {
        System.out.println("UsersServlet");
    }

    @Override
    public void destroy() {
        System.out.println("UsersServlet销毁");
    }

    @Override
    public void init() throws ServletException {
        System.out.println("UsersServlet初始化");
    }

    @Override   //接收get请求:超链接
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //接收用户传递过来的参数
        String uage = req.getParameter("uage");
        String upass = req.getParameter("upass");
        System.out.println(uage+","+upass);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //session
        HttpSession session= req.getSession();
        session.setAttribute("k1","aaa");
        session.getAttribute("k1");
        session.removeAttribute("k1");
        String id = session.getId();
        System.out.println(id);

        //处理post请求的乱码
        req.setCharacterEncoding("utf-8");

        //接收用户传递过来的参数
        //无论前台传递什么格式的数据,后台都是使用String接收的
        String username = req.getParameter("username");
        //接收一组数据
        String[] aihaos = req.getParameterValues("aihao");
        for(String aihao:aihaos){
            System.out.println(aihao);
        }
        System.out.println("post->"+username);
        //前台给后台数据:req.getParameter();
        //后台给前台数据:req.setAttribute("key",value);
        //request存值是单词请求有效
        req.setAttribute("uname",username);
        req.setAttribute("aihao",aihaos);
        Object uname = req.getAttribute("uname");
        //跳转页面
        //转发
        //req.getRequestDispatcher("/success.html").forward(req,resp);
        //跳转页面
        //重定向
        resp.sendRedirect("/success.html");
        //响应:服务器端给客户端信息
        resp.setContentType("text/html");//设置响应的内容
        //resp.addCookie();//设置cookie值存储
        PrintWriter writer = resp.getWriter();//得到一个可以给前端输出信息的输出流对象
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值