使⽤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 =Writer();
String Parameter("uname");
String Parameter("email");
String Parameter("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中的l添加如下配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="/xml/ns/javaee"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/xml/ns/javaee /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
在浏览器输⼊测试地址:
>铃兰
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论