Jsp和Servlet复习

Posted SmallCuteMonkey

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Jsp和Servlet复习相关的知识,希望对你有一定的参考价值。

package com.hou;

import javax.jws.WebService;
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;
import java.io.PrintWriter;


//@WebService(serviceName = "hello")  是WebServlet 注解




//生命周期
//1. 进行初始化   2。执行 3.进行销毁
@WebServlet(urlPatterns = "/login",name = "",loadOnStartup = 1)
public class Servlet extends HttpServlet {

//    生命周期  init() service() destory()
//    LoadOnstartup 加载时机
    @Override
    public void init() throws ServletException {



        System.out.println("方法正在被执行");
    }

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

//        设置请求的乱码
        req.setCharacterEncoding("utf-8");
//        设置响应的乱码
        resp.setContentType("text/html,charset=utf-8");
//        响应的输出流的编码
        resp.setCharacterEncoding("utf-8");


        String username = req.getParameter("username");
        String password = req.getParameter("password");
        System.out.println("username=" + username+"password"+password);


//        把输出的内容响应到浏览器上面
        PrintWriter pw=resp.getWriter();

        pw.println("username=="+username+"password=="+password);
        pw.flush();
        pw.close();


    }


   @Override
    public void destroy(){
        super.destroy();

        System.out.println("方法被销毁");
    }
}

Login.jsp:

<%--
  Created by IntelliJ IDEA.
  User: CourageAndLove
  Date: 2021/7/7
  Time: 10:21
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登录页面</title>
</head>
<body>


<form action="login" method="post">

    用户名: <input type="text" name="username"><br>
    密码: <input type="password" name="password" id=""><br>
    <input type="submit" value="提交">


</form>




</body>
</html>

以上是关于Jsp和Servlet复习的主要内容,如果未能解决你的问题,请参考以下文章

复习JSP/Servlet技术

JSP Web第七章整理复习 Servlet基础知识

JavaWeb--深入Servlet与JSP(运行原理)

jsp初识servlet转发转发与重定向的比较

Java基础——JSP

Tomcat根据JSP生成Servlet机制解析