Web技术
JSP + Servlet 编程。
请在你的计算机上安装JDK和Tomcat,并按如下要求完成程序开发:
第一步:编写用户登录页面login3.jsp,要求使用page指令设置页面的字符编码方式为utf-8,并且页面包含一个表单,表单允许用户输入用户名和密码,并有“提交”和“重置”按钮。
第二步:编写一个Servlet程序Login3_do,接收login3.jsp发来的表单数据,把接收到的数据返回给用户。
完成程序开发后,回答下述问题:
(1)写出Servlet编译环境和编译方法。服务器端小程序,容器中运行,由容器控制
(2)写出login3.jsp的源代码。
(3)写出Servlet程序Login3_do.java的源程序代码。
(4)写出Tomcat上的web.xml文件的内容。
(1)Servlet 编译环境:JDK8+Tomcat,编译方法将项目打成 war 包,放到 Tomcat 的 webapp 目录下,然后启动 Tomcat,打开浏览器访问 localhost:8080/login3.jsp
(2)
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>用户登录</title>
<link href="css/bootstrap.min.css" rel="stylesheet">
<script src="js/jquery-2.1.0.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<style>
#login {
width: 400px;
}
h3 {
text-align: center;
}
#btn {
text-align: center;
}
</style>
</head>
<body>
<div class="container" id="login">
<h3>用户登录</h3>
<form action="${pageContext.request.contextPath}/login3_do" method="post">
<div class="form-group">
<label for="admin">用户名:</label>
<input type="text" name="name" class="form-control" id="admin" placeholder="请输入用户名"/>
</div>
<div class="form-group">
<label for="password">密码:</label>
<input type="password" name="password" class="form-control" id="password" placeholder="请输入密码"/>
</div>
<hr/>
<div class="form-group" id="btn">
<input class="btn btn-primary" type="submit" value="登录">
<input class="btn btn-default" type="reset" value="重置" />
</div>
</form>
</div>
</body>
</html>
(3)
public class Login3_do extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
//1.设置编码
request.setCharacterEncoding("utf-8");
//2.获取用户信息
String name = request.getParameter("name");
String password = request.getParameter("password");
// 3.返回给用户
request.setAttribute("name", name);
request.setAttribute("password", password);
// 4.跳转首面
response.sendRedirect(request.getContextPath() + "/index.jsp");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
this.doPost(request, response);
}
}
(4)
<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>
<!--给 Servlet 起一个名字,一般和类名一致-->
<servlet-name>Login3_do</servlet-name>
<!--配置这个 Servlet 的全类名-->
<servlet-class>com.zt.web.servlet.Login3_do/servlet-class>
</servlet>
<!--配置 Servlet 的映射路径-->
<servlet-mapping>
<servlet-name>Login3_do</servlet-name>
<!--外界能访问到 Servlet 的资源路径-->
<url-pattern>/login3_do</url-pattern>
</servlet-mapping>
</web-app>
servlet中不需要使用数据库保存数据吧。
其实就一个问题,就是如何使用servlet编写一个登录功能的web界面,两种方式,传入的账号密码在servlet中写死进行比对,或者在数据库里面查询,将结果返回给前端界面
我帮你写,私信我,我发项目给你。
您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
ps: 问答会员年卡【8折】购 ,限时加赠IT实体书,即可 享受50次 有问必答服务,了解详情>>>https://t.csdnimg.cn/RW5m