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

非常感谢孙浩老师。

 

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, HttpServletResponse resp)
			throws ServletException, 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='删除' οnclick="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,keyword2,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,keyword2,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:

登入:

删除:

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值