1.新建web工程,此处不多做赘述,具体步骤点击下方链接:
https://blog.csdn.net/Python_Smily/article/details/105532833
新建的工程名为·:Demo0403,名字可随意定义;
2.在webroot下,建立html文件,右键单击“WebRoot”;
3.New—>HTML
4.File Name自定义设置,此处我设为index.html;
5.而后,可看到WebRoot下有index.html;
6.接下来,写代码;
源代码:
<!DOCTYPE html>
<html>
<head>
<title>index.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body bgcolor="pink">
<center>
<td colspan="2"><center><font face="楷体" size="6" color="blue">登录界面:</font></td> <br>
<table border="1">
<form action="/Demo0403/Fs1" method="post">
<tr>
<td colspan="3">姓名:<input type="text" name="username"></td><br>
</tr>
<tr>
<td colspan="3">密码:<input type="password" name="password"></td><br>
</tr>
<tr>
<td colspan="3">性别:<input type="radio" name="sex" value="男">男
<input type="radio" name="sex" value="女">女</td><br>
</tr>
<tr>
<td colspan="3">爱好:<input type="checkbox" name="hobby" value="书法">书法
<input type="checkbox" name="hobby" value="羽毛球">羽毛球
<input type="checkbox" name="hobby" value="马拉松长跑">马拉松长跑
<input type="checkbox" name="hobby" value="五子棋">五子棋
<input type="checkbox" name="hobby" value="轮滑">轮滑</td><br>
</tr>
<tr>
<td colspan="3"><input type="submit" value="提交"></td>
</tr>
</form>
</table>
</body>
</html>
代码界面:
结果输出界面:
7.配置WEB-INF下的web.xml文件;双击web.xml,点击source显示源码;
源码显示如下图;
编写源代码后如图:
源代码:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<servlet>
<servlet-name>Fs1</servlet-name>
<servlet-class>mm.nn.Fs1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Fs1</servlet-name>
<url-pattern>/Fs1</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
8.登录之后如何接收?!在src文件夹下,单击右键,new新建,选择新建一个class文件,
Package名设为:mm.nn;Name设为Fs1;而后,Finish;
编写代码,注意右键点击类名,Source,Override;
doGet、doPost方法可以替代service方法;
HttpServletRequest req //请求
HttpServletResponse resp //响应
想要接收这个信息:
姓名:<input type="text" name="username">
可以使用request请求对象的getParameter();方法
编写完代码如图:
源代码分享:
package mm.nn;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Fs1 extends HttpServlet{
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/html;charset=utf-8");//解决响应数据乱码问题
req.setCharacterEncoding("utf-8");//解决传递参数乱码
//接收index.html传过来的参数信息
String username=req.getParameter("username");
String password=req.getParameter("password");
String sex=req.getParameter("sex");//执行出来效果在控制台显示
//输入输出流、字符流
PrintWriter out=resp.getWriter();String[] hobby=req.getParameterValues("hobby");
out.println("<br>您的爱好:");
for(int i=0;i<hobby.length;i++){
out.println(hobby[i]+",");
}
/*out.println("用户名为:"+username);
out.println("密码为:"+password);
out.println("性别:"+sex);*/
String[] name={"孙悦","孙锐","孙熙然","孙昊然"};
List<String> list=Arrays.asList(name);
if(list.contains(username) && password!=null &&password.length()>=6){
out.println("用户名为:"+username);
out.println("密码为:"+password);
out.println("性别:"+sex);
}
else{
out.println("您的用户名不存在,请重新登录,正在跳转,请稍等......");
//重定向,几秒钟后跳转某个页面
resp.setHeader("Refresh", "10;URL=/Demo0403/index.html");
}
}
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
this.doGet(req, resp);
}
}
9.编写完代码后运行,利用Tomcat运行结果;
如何运行不做多的赘述,详情请见https://blog.csdn.net/Python_Smily/article/details/105426637;
10.直接得结果;
- 登录用户名合法
- 登录用户名不合法
10s后,跳转到登录界面;
至此结束。
pretty good!