初入Java后端之Servlet

初入Java后端之Servlet

什么是Servlet?

Servlet实际上是一个按照Servlet规范写的Java类。是运行在Web服务端的Java应用程序。与Java程序的区别是,它里面封装了对Http请求的处理。

功能

Servlet主要是对Http请求进行相应的处理,生成动态的Web内容。

实现

原理图

至于Servlet的细节原理和优点,先暂时不说,直接上代码

总的来说,后端主要是处理前段发送过来的请求,那么最常见的请求便是post请求和get请求。

Servlet处理Get请求

前段Get请求表单,创建一个1.jsp文件

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>初入Servlet</title>
</head>
<body>

<%--使用Get请求,请求的url为/test--%>
<form method="get" action="/test">
    <label>账号</label>
    <input type="text" name="name" />
    <label>密码</label>
    <input type="password" name="pwd" />
</form>
</body>
</html>

页面样式:

后端处理Get请求

package com.server;

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;

//urlPatterns = {"/test"} 代表请求的路由
@WebServlet(name = "test",urlPatterns = {"/test"})
public class Test extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    //处理get请求的函数
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获得账号
        String name  = request.getParameter("name");
        //获得账号
        String pwd = request.getParameter("pwd");
        //设置响应编码为utf-8
        response.setCharacterEncoding("utf-8");
        //告知浏览器编码方式; 浏览器默认编码是GBK
        response.setHeader("Content-type", "text/html;charset=UTF-8");

        PrintWriter writer = response.getWriter();
        writer.print("账号是"+name+","+"密码是"+pwd);
    }
}

结果:

后端处理Post请求:
前端只要将method="get"==>method="post"

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>初入Servlet</title>
</head>
<body>

<%--使用Get请求,请求的url为/test--%>
<form method="post" action="/test">
    <label>账号</label>
    <input type="text" name="name" />
    <label>密码</label>
    <input type="password" name="pwd" />
    <input type="submit" value="登录"/>

</form>
</body>
</html>

后端的请求与doGet一样,所以可以在doPost请求中调用doGet请求,代码如下

package com.server;

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;

//urlPatterns = {"/test"} 代表请求的路由
@WebServlet(name = "test",urlPatterns = {"/test"})
public class Test extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //使用doGet函数处理post请求
        this.doGet(request,response);
    }

    //处理get请求的函数
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获得账号
        String name  = request.getParameter("name");
        //获得账号
        String pwd = request.getParameter("pwd");
        //设置响应编码为utf-8
        response.setCharacterEncoding("utf-8");
        //告知浏览器编码方式; 浏览器默认编码是GBK
        response.setHeader("Content-type", "text/html;charset=UTF-8");

        PrintWriter writer = response.getWriter();
        writer.print("账号是"+name+","+"密码是"+pwd);
    }
}

这次是对Java Servlet简单使用的介绍,下次会介绍更多的关于Servlet的使用

posted @ 2018-10-28 22:25  渣渣辉啊  阅读(1221)  评论(0编辑  收藏  举报