Servlet学习笔记

x33g5p2x  于2021-12-07 转载在 其他  
字(4.8k)|赞(0)|评价(0)|浏览(429)

用 Servlet 实现:在浏览器上输入地址 http://localhost:8080/project/hello 就能在页面上显示一个字符串「Hello Servlet」。

首先新建一个maven项目:webapp,然后完成一些初始操作:

然后新建servlet类:导入HttpServlet 然后重写方法:

package com.ftz.Demo.Servlet;

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

/** * @author ${范涛之} * @Description * @create 2021-12-06 23:44 */
public class Servlet1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter writer = resp.getWriter();   //响应流
        writer.print("Hello,Serlvet");

    }

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

在web.xml下面添加映射:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>



  <servlet>
    <servlet-name>Servlet</servlet-name>
    <servlet-class>com.ftz.Demo.Servlet.Servlet1</servlet-class>
  </servlet>

  <!--Servlet的请求路径-->
  <servlet-mapping>
    <servlet-name>Servlet</servlet-name>
    <url-pattern>/project/hello</url-pattern>
  </servlet-mapping>



</web-app>

实现效果:

新增一个 Servlet:在浏览器上输入地址 http://localhost:8080/project/world 就能在页面上显示一个字符串「Servlet World」

和上面同理只需要添加页面:

用 JSP 实现:在浏览器上输入地址 http://localhost:8080/project/hello.jsp 就能在页面上显示一个字符串「Hello JSP」。

用 JSP + JDBC 实现:用户在浏览器页面上提交用户名和密码后,将注册信息保存到数据库中,注册成功则返回成功页面,否则返回失败页面:

首先设计数据库:一个user表:

CREATE TABLE `user` (
  `id` int(11) NOT NULL,
  `username` varchar(255) DEFAULT NULL,
  `password` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

然后写前端界面,这里自己找一个就好,我用的是之前那个抓包任务的表单,然后写后端逻辑代码:

package com.ftz.Demo.Servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/** * @author ${范涛之} * @Description * @create 2021-12-07 1:51 */
public class LoginUp  extends HttpServlet {

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

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

        //设置服务器端以UTF-8编码进行输出
        resp.setCharacterEncoding("UTF-8");
        //设置浏览器以UTF-8编码进行接收,解决中文乱码问题
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        /** * 获取前端传来的值 */
        String username = req.getParameter("username");
        String passwd = req.getParameter("passwd");

        String url = "jdbc:mysql://localhost:3306/servletstudy";
        String name = "root";
        String pwd = "root";
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection connection = DriverManager.getConnection(url,name,pwd);
            String sql = "insert into user \n"+
                    "values\n" +
                    "(1,"+username+','+passwd+");";
                    ;

            Statement statement =connection.createStatement();
// 执行sql语句
            int temp = statement.executeUpdate(sql);
            System.out.println("返回的值:"+temp);
            statement.close();
            connection.close();
            resp.sendRedirect("/success.jsp");

        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }

    }
}

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
         xmlns="http://java.sun.com/xml/ns/j2ee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">


    <servlet>
        <servlet-name>Servlet</servlet-name>
        <servlet-class>com.ftz.Demo.Servlet.Servlet1</servlet-class>
    </servlet>
    <!--Servlet的请求路径-->
    <servlet-mapping>
        <servlet-name>Servlet</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>World</servlet-name>
        <servlet-class>com.ftz.Demo.Servlet.Servlet2</servlet-class>
    </servlet>
    <!--Servlet的请求路径-->
    <servlet-mapping>
        <servlet-name>World</servlet-name>
        <url-pattern>/world</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>Loginup</servlet-name>
        <servlet-class>com.ftz.Demo.Servlet.LoginUp</servlet-class>
    </servlet>
    <!--Servlet的请求路径-->
    <servlet-mapping>
        <servlet-name>Loginup</servlet-name>
        <url-pattern>/LoginUp/Register</url-pattern>
    </servlet-mapping>

</web-app>

上图:

相关文章