加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Webservice_25_SOAP的基于契约优先开发用户管理_实现Jsp页面功能

发布时间:2020-12-17 00:11:09 所属栏目:安全 来源:网络整理
导读:非常感谢孙浩老师。 ? servlet: package cn.lichen.servlet;import java.io.IOException;public class UserServlet extends HttpServlet {private static final long serialVersionUID = 1L;private UserService us;private IUserService service;@Override

非常感谢孙浩老师。

?

servlet:

package cn.lichen.servlet;

import java.io.IOException;

public class UserServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	private UserService us;
	private IUserService service;

	@Override
	protected void doGet(HttpServletRequest req,HttpServletResponse resp)
			throws ServletException,IOException {
		req.setCharacterEncoding("UTF-8");
		us = new UserService();
		service = us.getUserServicePort();
		// 标识不同的方法
		String method = req.getParameter("method");
		if (method == null || "".equals(method)) {
			list(req,resp);
		} else if (method.equals("add")) {
			add(req,resp);
		} else if (method.equals("login")) {
			login(req,resp);
		} else if (method.equals("delete")) {
			delete(req,resp);
		}

	}

	private void delete(HttpServletRequest req,HttpServletResponse resp) {
		String username = req.getParameter("username");
		service.delete(username);

		list(req,resp);
	}

	private void login(HttpServletRequest req,HttpServletResponse resp) {
		String username = req.getParameter("username");
		String password = req.getParameter("password");
		try {
			req.setAttribute("user",service.login(username,password));
		} catch (UserException_Exception e) {
			e.printStackTrace();
		}

		list(req,resp);
	}

	private void add(HttpServletRequest req,HttpServletResponse resp) {
		User user = new User();
		user.setUsername(req.getParameter("username"));
		user.setNickname(req.getParameter("nickname"));
		user.setPassword(req.getParameter("password"));
		try {
			service.add(user);
		} catch (UserException_Exception e) {
			e.printStackTrace();
		}

		list(req,resp);
	}

	@Override
	protected void doPost(HttpServletRequest req,IOException {
		doGet(req,resp);
	}

	private void list(HttpServletRequest request,HttpServletResponse response) {
		try {
			request.setAttribute("users",service.list());
			RequestDispatcher dis = request.getRequestDispatcher("list.jsp");
			dis.forward(request,response);
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}


list.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<style type="text/css">
table {
	width: 50%;
	border-top: 1px solid black;
	border-left: 1px solid black;
	text-align: center;
	letter-spacing: 3px;
}

th {
	background-color: #00ff90;
}

th,td {
	border-bottom: 1px solid black;
	border-right: 1px solid black;
}
</style>
<body>
	当前用户:${requestScope.user.nickname}
	<br>

	<table id="tt" class="usertable" cellspacing="0" cellpadding="0">
		<tr>
			<th>用户名</th>
			<th>呢称</th>
			<th>密码</th>
			<th>操作</th>
		</tr>
		<c:forEach var="user" items="${requestScope.users}">
			<tr>
				<td>${user.username }</td>
				<td>${user.nickname }</td>
				<td>${user.password }</td>
				<td><input type='button' value='删除' onclick="javascript:location.href='user.do?username=${user.username }&method=delete'"/></td>
			</tr>
		</c:forEach>
	</table>
</body>
</html>

?

add.jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword3">
<meta http-equiv="description" content="This is my page">
</head>

<body>
	当前用户:${requestScope.user.nickname}
	<br>

<form action="user.do" method="post">
	<input type="hidden" name="method" value="add">
	UserName:<input type="text" name="username"/><br/>
	Password:<input type="text" name="password"/><br/>
	Nickname:<input type="text" name="nickname"/><br/>
	<input type="submit">
</form>
</body>
</html>

?

login:

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword3">
<meta http-equiv="description" content="This is my page">
</head>

<body>
	当前用户:${requestScope.user.nickname}
	<br>
<form action="user.do" method="post">
	<input type="hidden" name="method" value="login">
	UserName:<input type="text" name="username"/><br/>
	Password:<input type="text" name="password"/><br/>
	<input type="submit">
</form>
</body>
</html>


得到所有user信息:

添加user:

登入:

删除:

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读