zoukankan      html  css  js  c++  java
  • 使用IDEA 创建简单servlet表单项目实例

    • 选择new->project,如图

    • 命名mypro

    • 在WEB-INF文件下创建classes和lib两个文件夹

    • 更改项目输出为刚才创建的classes

    • 在web上右键新建register.jsp,创建jsp表单提交页面
    <%--
      Created by IntelliJ IDEA.
      User: TOM
      Date: 2020/11/14
      Time: 19:14
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <html>
    <head>
        <title>post表单提交</title>
    </head>
    <body>
    <form action="RegServlet" method="post">
        请输入信息:
        <table>
            <tr>
                <td>客户名:</td>
                <td><input type="text" name="uname" id="uname"/></td>
            </tr>
            <tr>
                <td>邮箱地址:</td>
                <td><input type="text" name="email" id="email"/></td>
            </tr>
            <tr>
                <td>电话:</td>
                <td><input type="text" name="mobile" id="mobile"/></td>
            </tr>
    
        </table>
        <input type="submit" value="确定"/>
        <input type="reset" value="重置"/>
    </form>
    
    </body>
    </html>
    • 在src上右键,新创建servlet类

    package com.howdy;
    
    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;
    
    @WebServlet(name = "RegServlet")
    public class RegServlet extends HttpServlet {
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            request.setCharacterEncoding("UTF-8");
            response.setContentType("text/html;charset=utf-8");
            PrintWriter out =response.getWriter();
            String uname=request.getParameter("uname");
            String email=request.getParameter("email");
            String mobile=request.getParameter("mobile");
            out.println("<html>");
            out.println("<body>");
            out.println("<h3>您的名字是:"+uname+" </h3>");
            out.println("<h3>您的email是:"+email+" </h3>");
            out.println("<h3>您的电话是:"+mobile+" </h3>");
            out.println("</body>");
            out.println("</html>");
        }
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
        }
    }
    • 在web中的web.xml添加如下配置
    <?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>RegServlet</servlet-name>
            <servlet-class>com.howdy.RegServlet</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>RegServlet</servlet-name>
            <url-pattern>/RegServlet</url-pattern>
        </servlet-mapping>
        <!--    <servlet-mapping>-->
        <!--        <servlet-name>RegServlet</servlet-name>-->
        <!--        <url-pattern>/mypro/RegServlet</url-pattern>-->
        <!--    </servlet-mapping>-->
        </web-app>

      注意此地方是两种写法:

     <url-pattern>/mypro/RegServlet</url-pattern> --->form的action应该是mypro/RegServlet
    
     <url-pattern>/RegServlet</url-pattern> --->form的action应该是RegServlet
    • 运行项目时,单击edit configuration

    • 在浏览器输入测试地址:

  • 相关阅读:
    计算机基础学习路线路线详情:4步骤·5门课
    python课程体系是怎么样的?
    老猿学5G扫盲贴:中国移动5G融合计费漫游计费架构和路由方案
    老猿学5G扫盲贴:中国移动网络侧CHF主要功能及计费处理的主要过程
    moviepy音视频剪辑:moviepy中的剪辑相关类及关系
    老猿学5G扫盲贴:与用户和终端相关的名词UE、SUPI、GPSI、PEI
    PyQt+moviepy音视频剪辑实战文章目录
    PyQt+moviepy音视频剪辑实战2:一个剪裁视频文件精华内容留存工具的实现
    PyQt+moviepy音视频剪辑实战2:实现一个剪裁视频文件精华内容留存工具
    PyQt+moviepy音视频剪辑实战1:多个音视频合成顺序播放或同屏播放的视频文件实现详解
  • 原文地址:https://www.cnblogs.com/personblog/p/13974491.html
Copyright © 2011-2022 走看看