J2EE实验6:Servlet与MVC

实验6:Servlet与MVC

实验要求:

将实验5改造为符合Model 2的MVC结构程序,并实现好友的添加、删除、修改功能。



DataBaseOperate.java

package com.fieldsoft.DAO;
import java.sql.*;
import java.util.*;

public class DataBaseOperate {
	static public void main(String[] args){
		
		
	}
	
	/**
	 * 
	 * @param UserName
	 * @param PassWord
	 * @return 1:注册成功 0:用户名已经存在
	 * @throws SQLException 
	 * @throws ClassNotFoundException 
	 */
	static public int RegisterUser(String UserName,String PassWord) throws SQLException, ClassNotFoundException{
		//加载驱动
		Class.forName("com.mysql.jdbc.Driver");
		//连接数据库
		Connection DataConnection = java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/myfriend","root","12345678");
		Statement DataStatement = DataConnection.createStatement();
		//查询用户名以否已经存在
		ResultSet UserIsExistResult = DataStatement.executeQuery("select name from user where name = '" + UserName + "'");
		if (UserIsExistResult.next()){
			//存在
			return 0;
		}
		else{
			//不存在,添加
			//获取最大id
			ResultSet MaxIdResult = DataStatement.executeQuery("select max(userid) from user");
			int MaxId;
			if (MaxIdResult.next()){
				MaxId = MaxIdResult.getInt("max(userid)");
			}
			else{
				MaxId = 0;
			}
			MaxId ++;
			DataStatement.executeUpdate("INSERT INTO user VALUES ('" + MaxId + "', '" + UserName + "', '" + PassWord + "')");
			return 1;
		}
	}
	
	/**
	 * 
	 * @param UserName
	 * @param PassWord
	 * @return 0:登录成功 1:用户名不存在 2:密码不正确
	 * @throws ClassNotFoundException 
	 * @throws SQLException 
	 */
	static public int LoginUser(String UserName,String PassWord) throws ClassNotFoundException, SQLException{
		//加载驱动
		Class.forName("com.mysql.jdbc.Driver");
		//连接数据库
		Connection DataConnection = java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/myfriend","root","12345678");
		Statement DataStatement = DataConnection.createStatement();
		//查询用户名以否已经存在
		ResultSet UserIsExistResult = DataStatement.executeQuery("select * from user where name = '" + UserName + "'");
		if (UserIsExistResult.next()){
			String DBPassWord = UserIsExistResult.getString("password");
			if (DBPassWord.equals(PassWord)){
				//登录成功
				return 0;
			}
			else{
				return 2;
			}
		}
		else{
			return 1;
		}
	}
	
	static public String[] GetFriend(String FriendId) throws ClassNotFoundException, SQLException{
		//加载驱动
		Class.forName("com.mysql.jdbc.Driver");
		//连接数据库
		Connection DataConnection = java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/myfriend","root","12345678");
		Statement DataStatement = DataConnection.createStatement();
		//查询用户名关联的朋友
		ResultSet MyFriendsResult = DataStatement.executeQuery(
				"select * from myfriend where id = '" + FriendId + "'");
		if (MyFriendsResult.next()){
			String[] RowStringArray = new String[9];
			RowStringArray[0] = String.valueOf(MyFriendsResult.getInt("id"));
			RowStringArray[1] = String.valueOf(MyFriendsResult.getInt("userid"));
			RowStringArray[2] = String.valueOf(MyFriendsResult.getString("name"));
			RowStringArray[3] = String.valueOf(MyFriendsResult.getString("sex"));
			RowStringArray[4] = String.valueOf(MyFriendsResult.getInt("age"));
			RowStringArray[5] = String.valueOf(MyFriendsResult.getString("qq"));
			RowStringArray[6] = String.valueOf(MyFriendsResult.getString("telephone"));
			RowStringArray[7] = String.valueOf(MyFriendsResult.getString("email"));
			RowStringArray[8] = String.valueOf(MyFriendsResult.getString("address"));
			return RowStringArray;
		}
		return null;
	}
	
	/**
	 * 
	 * @param UserName
	 * @return
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 */
	static public String[][] GetAllFriends(String UserName,String SubName) throws ClassNotFoundException, SQLException{
		//System.out.println(UserName + SubName);
		//加载驱动
		Class.forName("com.mysql.jdbc.Driver");
		//连接数据库
		Connection DataConnection = java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/myfriend","root","12345678");
		Statement DataStatement = DataConnection.createStatement();
		//查询用户名关联的朋友
		ResultSet MyFriendsResult = DataStatement.executeQuery(
				"select * from myfriend where userid in (select userid from user where name = '" + UserName + "')");
		if (MyFriendsResult.next()){
			MyFriendsResult.beforeFirst();
			ArrayList<String[]> ResultArray = new ArrayList<String[]>();
			while (MyFriendsResult.next()){
				String[] RowStringArray = null;
				String TempName = String.valueOf(MyFriendsResult.getString("name"));
				if (SubName == "" || TempName.matches("^.*" + SubName + ".*$")){
					RowStringArray = new String[9];
					RowStringArray[0] = String.valueOf(MyFriendsResult.getInt("id"));
					RowStringArray[1] = String.valueOf(MyFriendsResult.getInt("userid"));
					RowStringArray[2] = String.valueOf(MyFriendsResult.getString("name"));
					RowStringArray[3] = String.valueOf(MyFriendsResult.getString("sex"));
					RowStringArray[4] = String.valueOf(MyFriendsResult.getInt("age"));
					RowStringArray[5] = String.valueOf(MyFriendsResult.getString("qq"));
					RowStringArray[6] = String.valueOf(MyFriendsResult.getString("telephone"));
					RowStringArray[7] = String.valueOf(MyFriendsResult.getString("email"));
					RowStringArray[8] = String.valueOf(MyFriendsResult.getString("address"));
					ResultArray.add(RowStringArray);
				}
			}
			String[][] Result = new String[ResultArray.size()][9];
			for (int i = 0;i < ResultArray.size();i ++){
				Result[i] = ResultArray.get(i);
			}
			return Result;
		}
		else{
			return null;
		}
	}
	
	static public int Insert(String UserName,String NewName,String NewSex,String NewAge,String NewQQ,String NewTel,String NewMail,String NewAddr) throws ClassNotFoundException, SQLException{
		//加载驱动
		Class.forName("com.mysql.jdbc.Driver");
		//连接数据库
		Connection DataConnection = java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/myfriend","root","12345678");
		Statement DataStatement = DataConnection.createStatement();
		//查询userid
		ResultSet UserIdResult = DataStatement.executeQuery(
				"select userid from user where name = '" + UserName + "'");
		String userid;
		if (UserIdResult.next()){
			userid = UserIdResult.getString("userid");
		}
		else{
			return -1;
		}
		//获取最大值
		ResultSet MaxIdResult = DataStatement.executeQuery("select max(id) from myfriend");
		int MaxId;
		if (MaxIdResult.next()){
			MaxId = MaxIdResult.getInt("max(id)");
		}
		else{
			MaxId = 0;
		}
		MaxId ++;
		System.out.println(MaxId);
		//插入新纪录
		DataStatement.executeUpdate(
				"INSERT INTO myfriend VALUES ('" + MaxId + "', '" + userid + "', '" + NewName + "', '" + NewSex + "', '" + NewAge + "', '" + NewQQ + "', '" + NewTel + "', '" + NewMail + "', '" + NewAddr + "')");
		return 0;
	}
	
	/**
	 * 
	 * @param FriendId
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 */
	static public void DeleteFriend(String FriendId) throws ClassNotFoundException, SQLException{
		//加载驱动
		Class.forName("com.mysql.jdbc.Driver");
		//连接数据库
		Connection DataConnection = java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/myfriend","root","12345678");
		Statement DataStatement = DataConnection.createStatement();
		//删除id
		DataStatement.executeUpdate(
				"delete from myfriend where id = '" + FriendId + "'");
	}
	
	static public int Modify(String FriendId,String NewName,String NewSex,String NewAge,String NewQQ,String NewTel,String NewMail,String NewAddr) throws ClassNotFoundException, SQLException{
		//加载驱动
		Class.forName("com.mysql.jdbc.Driver");
		//连接数据库
		Connection DataConnection = java.sql.DriverManager.getConnection("jdbc:mysql://localhost:3306/myfriend","root","12345678");
		Statement DataStatement = DataConnection.createStatement();
		//插入新纪录
		DataStatement.executeUpdate(
				"UPDATE myfriend SET name='" + NewName + "', sex='" + NewSex + "', age='" + NewAge + "', qq='" + NewQQ + "', telephone='" + NewTel + "', email='" + NewMail + "', address='" + NewAddr + "' WHERE id = '" + FriendId + "' ");
		return 0;
	}
}

没有用连接池,代码与实验5一致。


AccountFilter.java

package com.fieldsoft.Filter;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.*;
import javax.servlet.http.*;

public class AccountFilter implements Filter {

	@Override
	public void destroy() {

	}

	@Override
	public void doFilter(ServletRequest mServletRequest, ServletResponse mServletResponse,
			FilterChain mFilterChain) throws IOException, ServletException {
		//初始化
		HttpServletRequest request = (HttpServletRequest) mServletRequest;
		HttpServletResponse response = (HttpServletResponse) mServletResponse;
		HttpSession session = request.getSession();
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		
		if (session != null){
			//获取当前请求页面
			String RequestURI = request.getRequestURI();
			String ContextPath = request.getContextPath();
			Object LoginedUserName = session.getAttribute("LoginedUserName");
			if (LoginedUserName == null){
				//未登录
				if (RequestURI.equals(ContextPath + "/LoginForm.jsp") ||
						RequestURI.equals(ContextPath + "/RegisterForm.jsp") ||
						RequestURI.equals(ContextPath + "/servlet/AccountServlet")){
					//只能去登录和注册
					mFilterChain.doFilter(mServletRequest, mServletResponse);
				}
				else{
					out.print("请先登录!<br/> <a href='" + ContextPath + "/LoginForm.jsp' />登录</a>");
			  		out.close();
				}
			}
			else{
				//登录了不能去LoginForm.jsp,显示已经登录
				if (RequestURI.equals(ContextPath + "/LoginForm.jsp")){
					out.print("您已经登录了!<br/> <a href='index.jsp' />主页</a>");
					out.close();
				}
				else{
					mFilterChain.doFilter(mServletRequest, mServletResponse);
				}
			}
		}
		else{
			throw new ServletException();
		}
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {

	}

}

Filter,用于哪些网页可以访问,哪些不可以。这里用于控制登录用户与不登陆状态访问页面的控制。


AccountServlet.java

package com.fieldsoft.Servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.fieldsoft.DAO.DataBaseOperate;

public class AccountServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public AccountServlet() {
		super();
	}

	/**
	 * .3 Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy();
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		HttpSession session = request.getSession();
		String ControlerType = request.getParameter("type");
		if (ControlerType.equals("Logout")) {
			if (session.getAttribute("LoginedUserName") == null) {
				out.print("未登录!<br/> <a href='" + request.getContextPath()
						+ "/LoginForm.jsp' />登录</a>");
			} else {
				session.removeAttribute("LoginedUserName");
				out.print("已经登出。<br/> <a href='" + request.getContextPath()
						+ "/LoginForm.jsp' />登录</a>");
			}
			out.close();
		}
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		HttpSession session = request.getSession();

		String ControlerType = request.getParameter("type");
		if (ControlerType != null) {
			try {
				if (ControlerType.equals("Login")) {
					String UserName = request.getParameter("UserName");
					String PassWord = request.getParameter("PassWord");
					if (UserName == null || PassWord == null) {
						response.sendRedirect("LoginForm.jsp");
					}

					int LoginUserResult;
					LoginUserResult = DataBaseOperate.LoginUser(UserName,
							PassWord);

					if (LoginUserResult == 0) {
						// 登录成功
						session.setAttribute("LoginedUserName",
								request.getParameter("UserName"));
						out.print("<h1>登录成功!</h1> <br /> <a href='"
								+ request.getContextPath()
								+ "/index.jsp' />主页</a>");
					} else if (LoginUserResult == 1) {
						out.print("<h1>密码不正确!</h1> <br /> <a href='"
								+ request.getContextPath()
								+ "/LoginForm.jsp' />重新登录</a>");
					} else if (LoginUserResult == 2) {
						out.print("<h1>密码不正确!</h1> <br /> <a href='"
								+ request.getContextPath()
								+ "/LoginForm.jsp' />重新登录</a>");
					}
					out.close();
				} else if (ControlerType.equals("Register")) {
					String UserName = request.getParameter("UserName");
					String PassWord = request.getParameter("PassWord");
					if (UserName == null || PassWord == null) {
						response.sendRedirect(request.getContextPath()
								+ "RegisterForm.jsp");
					} else {
						int RegisterResult;
						RegisterResult = DataBaseOperate.RegisterUser(UserName,
								PassWord);

						if (RegisterResult == 1) {
							out.print("<h1>注册成功!</h1> <br /> <a href='"
									+ request.getContextPath()
									+ "/LoginForm.jsp' />登录</a>");
						} else if (RegisterResult == 0) {
							out.print("<h1>用户名已经存在!</h1> <br /> <a href='"
									+ request.getContextPath()
									+ "/RegisterForm.jsp' />重新注册</a>");
						}
						out.close();
					}
				}
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		} else {
			response.sendRedirect(request.getContextPath() + "/index.jsp");
		}
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {

	}

}
用于控制账户登录、注册、登出等。

Servlet我暂时理解为符合java规则的jsp。


AllFriendControler.java

package com.fieldsoft.Servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.fieldsoft.DAO.DataBaseOperate;

public class AllFriendControler extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public AllFriendControler() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HttpSession session = request.getSession();
		String ControlerType = request.getParameter("type");
		if (ControlerType.equals("all")) {
			// RequestDispatcher
			// rd=request.getRequestDispatcher("/Exp006/allfriend.jsp");
			// rd.forward(request,response);
			response.sendRedirect(request.getContextPath() + "/allfriend.jsp");
		} else if (ControlerType.equals("query")) {
			response.sendRedirect(request.getContextPath() + "/querybyname.jsp");
		} else if (ControlerType.equals("insert")) {
			response.sendRedirect(request.getContextPath()
					+ "/InsertNewRowForm.jsp");
		}
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HttpSession session = request.getSession();
		String ControlerType = request.getParameter("type");
		if (ControlerType.equals("DataInsert")) {
			try {
				request.setCharacterEncoding("utf-8");
				String NewName = request.getParameter("NewName");
				String NewSex = request.getParameter("NewSex");
				String NewAge = request.getParameter("NewAge");
				String NewQQ = request.getParameter("NewQQ");
				String NewTel = request.getParameter("NewTel");
				String NewMail = request.getParameter("NewMail");
				String NewAddr = request.getParameter("NewAddr");
				if (NewName == null || NewSex == null || NewAge == null
						|| NewQQ == null || NewTel == null || NewMail == null
						|| NewAddr == null) {
					RequestDispatcher rd = request
							.getRequestDispatcher("/InsertNewRowForm.jsp");
					rd.forward(request, response);
				}
				DataBaseOperate.Insert(
						(String) session.getAttribute("LoginedUserName"),
						NewName, NewSex, NewAge, NewQQ, NewTel, NewMail,
						NewAddr);

				response.sendRedirect(request.getContextPath()
						+ "/allfriend.jsp");
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		} else if (ControlerType.equals("DataModify")) {
			try {
				request.setCharacterEncoding("utf-8");
				String NewName = request.getParameter("NewName");
				String NewSex = request.getParameter("NewSex");
				String NewAge = request.getParameter("NewAge");
				String NewQQ = request.getParameter("NewQQ");
				String NewTel = request.getParameter("NewTel");
				String NewMail = request.getParameter("NewMail");
				String NewAddr = request.getParameter("NewAddr");
				if (NewName == null || NewSex == null || NewAge == null
						|| NewQQ == null || NewTel == null || NewMail == null
						|| NewAddr == null) {
					response.sendRedirect(request.getContextPath()
							+ "/ModifyForm.jsp");
					return;
				}
				DataBaseOperate.Modify(request.getParameter("FriendId"),
						NewName, NewSex, NewAge, NewQQ, NewTel, NewMail,
						NewAddr);

				response.sendRedirect(request.getContextPath()
						+ "/allfriend.jsp");
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		} else if (ControlerType.equals("DeleteOrModify")) {
			String SubmitOperate = request.getParameter("SubmitOperate");
			if (SubmitOperate != null) {
				try {
					if (SubmitOperate.equals("Modify")) {
						// 获取修改的id
						String id = request.getParameter("Modify");
						System.out.println(id);
						response.sendRedirect(request.getContextPath()
								+ "/ModifyForm.jsp?id=" + id);
					} else if (SubmitOperate.equals("Delete")) {
						// 获取需要删除的id数组
						String[] CheckBoxs = request
								.getParameterValues("Delete");
						if (CheckBoxs != null) {
							for (int i = 0; i < CheckBoxs.length; i++) {
								DataBaseOperate.DeleteFriend(CheckBoxs[i]);
							}
						}
						response.sendRedirect(request.getContextPath()
								+ "/allfriend.jsp");
					}
				} catch (ClassNotFoundException e) {
					e.printStackTrace();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}

用于控制对于新增、修改、删除、查询等操作。


Servlet和Filter都需要在web.xml文件中配置。

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
    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_3_0.xsd">
  <filter>
  	<filter-name>AccountFilter</filter-name>
    <filter-class>com.fieldsoft.Filter.AccountFilter</filter-class>
  </filter>
  <filter-mapping>
  	<filter-name>AccountFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  
  <servlet>
    <servlet-name>AccountServlet</servlet-name>
    <servlet-class>com.fieldsoft.Servlet.AccountServlet</servlet-class>
  </servlet>
  <servlet>
    <servlet-name>AllFriendControler</servlet-name>
    <servlet-class>com.fieldsoft.Servlet.AllFriendControler</servlet-class>
  </servlet>



  <servlet-mapping>
    <servlet-name>AccountServlet</servlet-name>
    <url-pattern>/servlet/AccountServlet</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>AllFriendControler</servlet-name>
    <url-pattern>/servlet/AllFriendControler</url-pattern>
  </servlet-mapping>

</web-app>

每个filter和servlet都需要配置<servlet>、<servlet-mapping>,filter同理。

我暂时理解为<servlet>就是用来把java类与servlet名字联系起来,<servlet-mapping>用于把这个servlet与网页URL对应起来。

然后jsp跳转servlet的时候就使用web.xml配置好的虚拟URL。


比如:index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>主页</title>
  </head>
  <body>
	 <h1>我的好友录</h1><br/>
	 <p/>
	 <hr/>
	 <a href="servlet/AllFriendControler?type=all">查询所有好友信息</a><br/>
	 <a href="servlet/AllFriendControler?type=query">按姓名模糊查询好友信息</a><br/>
	 <a href="servlet/AccountServlet?type=Logout">登出</a><br/>
	 <hr/>
	 当前时间:<%=(new java.util.Date()).toString()%>
  </body>
</html>

MVC的好处就是条理很清晰,把前端与后端分开来了,不然JSP既有HTML又有JAVA代码真的很恼人。修改也方便。就是我原本以为所有的JSP都可以集中用一个Servlet控制,看样子是不能。


我把剩下的代码发一下

allfriend.jsp

<%@ page language="java" import="java.util.*,com.fieldsoft.DAO.*" pageEncoding="utf-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  	<title>查询所有好友信息</title>
  </head>
  
  <body>
	 <h1>我的好友录</h1><br/>
	 <a href="index.jsp">主页</a>
	 <hr/>
	 <form name='TableForm' action="servlet/AllFriendControler?type=DeleteOrModify" method="post">
		 <input type="hidden" name="SubmitOperate" value="Nothing">
		 <a href="servlet/AllFriendControler?type=insert">添加新纪录</a>
		 <a href="javascript:TableForm.submit()" οnclick="document.TableForm.SubmitOperate.value='Modify'";>修改选中记录</a>
		 <a href="javascript:TableForm.submit()" οnclick="document.TableForm.SubmitOperate.value='Delete'">删除选中记录</a>
		 <hr/>
		 	<%
	    	request.setCharacterEncoding("utf-8");
		 	//查询名字
		 	String SubStr = request.getParameter("FriendName");
		 	if (SubStr == null){
		 		SubStr = "";
		 	}

		 	String[][] FriendList = DataBaseOperate.GetAllFriends((String)session.getAttribute("LoginedUserName"),SubStr);
		 	%>
		 	<table width='100%' border='1'>
			 	<tr>
			 		<td><strong>修改</strong></td>
			 		<td><strong>删除</strong></td>
			 		<td><strong>姓名</strong></td>
			 		<td><strong>性别</strong></td>
			 		<td><strong>年龄</strong></td>
			 		<td><strong>QQ</strong></td>
			 		<td><strong>电话</strong></td>
			 		<td><strong>E-Mail</strong></td>
			 		<td><strong>地址</strong></td>
			 	</tr>
		 	<%
		 	if (FriendList != null){
				for (int i = 0;i < FriendList.length;i ++){
					out.print("<tr>");
					out.print("<td><input type='radio' name='Modify' value='" + FriendList[i][0] + "'/></td>");
					out.print("<td><input type='checkbox' name='Delete' value='" + FriendList[i][0] + "'/></td>");
					for (int j = 2; j < FriendList[i].length;j ++){
						out.print("<td>" + FriendList[i][j] + "</td>");
					}
					out.print("</tr>");
				}
			}
			else{
				%>
				<tr>
			 		<td colspan='9'>没有记录</td>
			 	</tr>
			 	<%
			}
			%>
			</table>
		</form>
	 	<hr/>
	 	当前时间:<%=(new java.util.Date()).toString()%>
  </body>
</html>

InsertNewRowForm.jsp

<%@ page language="java" import="java.util.*,com.fieldsoft.DAO.*" pageEncoding="utf-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  	<title>添加新纪录</title>
  </head>
  
  <body>
	 <script>
	 function insert(){
	 	if (document.NewRowForm.NewName.value == ""){
	 		alert("姓名不能为空!");
	 		return false;
	 	}
	 	else if (document.NewRowForm.NewAge.value == ""){
	 		alert("年龄不能为空!");
	 		return false;
	 	}
	 	return true;
	 }
	 </script>
	 <h1>我的好友录</h1><br/>
	 <a href="index.jsp">主页</a>
	 <hr/>
	 <form name='NewRowForm' action="servlet/AllFriendControler?type=DataInsert" method="post">
		<hr/>
		<table width='100%' border='1'>
			<tr>
				<td><strong>姓名</strong></td>
				<td><strong>性别</strong></td>
				<td><strong>年龄</strong></td>
				<td><strong>QQ</strong></td>
				<td><strong>电话</strong></td>
				<td><strong>E-Mail</strong></td>
				<td><strong>地址</strong></td>
			</tr>
			<tr>
				<td><input type="text" name="NewName"/></td>
				<td><input type="text" name="NewSex"/></td>
				<td><input type="text" name="NewAge"/></td>
				<td><input type="text" name="NewQQ"/></td>
				<td><input type="text" name="NewTel"/></td>
				<td><input type="text" name="NewMail"/></td>
				<td><input type="text" name="NewAddr"/></td>
			</tr>
			<tr>
				<td colspan="7" align="center"><input type="submit" value="添加新纪录" οnclick="return insert();"/></td>
		 	</tr>
		</table>
		</form>
	 	<hr/>
	 	当前时间:<%=(new java.util.Date()).toString()%>
  </body>
</html>

LoginForm.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>登录</title>
  </head>
  <body>
  	<script type="text/javascript">
	function login(){
		if (document.LoginForm.UserName.value == ""){
			alert("用户名不能为空!");
			return false;
		}
		return true;
	}
	</script>
	<h1>我的好友录</h1>
  	<hr/>
  	用户登录
  	<form name=LoginForm action="servlet/AccountServlet?type=Login" method="post">
  		<table border="0" width="100%">
  			<tr>
  				<td width="47%" align="right">登录名称</td>
  				<td><input type=text name=UserName></td>
  			</tr>
  			<tr>
  				<td align="right">登录密码</td>
  				<td><input type=password name=PassWord></td>
  			</tr>
  			<tr>
  				<td colspan="2" align="center">
  					<input type="submit" οnclick="return login();" value="登录">
  					<input type="reset" value="重置">
  					<input type="button" value="注册" οnclick="window.location.href='RegisterForm.jsp'">
  				</td>
			</tr>
  		</table>
  	</form>
  	<hr/>
  	当前时间:<%=(new java.util.Date()).toString()%>
  </body>
</html>

ModifyForm.jsp

<%@ page language="java" import="java.util.*,com.fieldsoft.DAO.*" pageEncoding="utf-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  	<title>添加新纪录</title>
  </head>
  
  <body>
	 <script>
	 function insert(){
	 	if (document.NewRowForm.NewName.value == ""){
	 		alert("姓名不能为空!");
	 		return false;
	 	}
	 	else if (document.NewRowForm.NewAge.value == ""){
	 		alert("年龄不能为空!");
	 		return false;
	 	}
	 	return true;
	 }
	 </script>
	 <%
	 String FriendId = request.getParameter("id");
	 if (FriendId == null){
	 	response.sendRedirect("index.jsp");
	 	return;
	 }
	 String[] FriendContent = DataBaseOperate.GetFriend(FriendId);
	 if (FriendContent == null){
	 	response.sendRedirect("allfriend.jsp");
	 	return;
	 }
	  %>
	 <h1>我的好友录</h1><br/>
	 <a href="index.jsp">主页</a>
	 <hr/>
	 <form name='NewRowForm' action="servlet/AllFriendControler?type=DataModify" method="post">
		<hr/>
		<input type="hidden" name="FriendId" value="<%=FriendId %>"/>
		<table width='100%' border='1'>
			<tr>
				<td><strong>姓名</strong></td>
				<td><strong>性别</strong></td>
				<td><strong>年龄</strong></td>
				<td><strong>QQ</strong></td>
				<td><strong>电话</strong></td>
				<td><strong>E-Mail</strong></td>
				<td><strong>地址</strong></td>
			</tr>
			<tr>
				<td><input type="text" name="NewName" value="<%=FriendContent[2]%>"/></td>
				<td><input type="text" name="NewSex" value="<%=FriendContent[3]%>"/></td>
				<td><input type="text" name="NewAge" value="<%=FriendContent[4]%>"/></td>
				<td><input type="text" name="NewQQ" value="<%=FriendContent[5]%>"/></td>
				<td><input type="text" name="NewTel" value="<%=FriendContent[6]%>"/></td>
				<td><input type="text" name="NewMail" value="<%=FriendContent[7]%>"/></td>
				<td><input type="text" name="NewAddr" value="<%=FriendContent[8]%>"/></td>
			</tr>
			<tr>
				<td colspan="7" align="center"><input type="submit" value="修改纪录" οnclick="return insert();"/></td>
		 	</tr>
		</table>
		</form>
	 	<hr/>
	 	当前时间:<%=(new java.util.Date()).toString()%>
  </body>
</html>

querybyname.jsp

<%@ page language="java" import="java.util.*,com.fieldsoft.DAO.*" pageEncoding="utf-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  	<title>按姓名模糊查询好友信息</title>
  </head>
  
  <body>
	<script>
		function FindByName(){
			if (document.QueryNameForm.FriendName.value == ""){
				alert("查询名字不能为空!");
				return false;
			}
			return true;
		}
	</script>
	<h1>我的好友录</h1><br/>
	<a href="index.jsp">主页</a>
	<hr/>
	<form name='QueryNameForm' action='allfriend.jsp' method='post'>
		输入要查询的名字:<input type='text' name='FriendName'/><input type='submit' οnclick='return FindByName();'/>
	</form>
	<hr/>
	当前时间:<%=(new java.util.Date()).toString()%>
  </body>
</html>

RegisterForm.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>注册</title>
  </head>
  <body>
	<script type="text/javascript">
	function register(){
		if (document.RegisterForm.UserName.value == ""){
			alert("用户名不能为空!");
			return false;
		}
		if (document.RegisterForm.PassWord.value.length < 6){
			alert("密码不能少于6位!");
			return false;
		}
		if (document.RegisterForm.PassWord.value != document.RegisterForm.PassWordAgain.value){
			alert("两次密码必须一致!");
			return false;
		}
		return true;
	}
	</script>
  	<h1>我的好友录</h1>
  	<hr/>
  	用户注册
  	<form name=RegisterForm action="servlet/AccountServlet?type=Register" method="post">
  		<table border="0" width="100%">
  			<tr>
  				<td width="45%" align="right">登录名称</td>
  				<td><input type=text name=UserName></td>
  			</tr>
  			<tr>
  				<td align="right">登录密码</td>
  				<td><input type=password name=PassWord></td>
  			</tr>
  			<tr>
  				<td align="right">密码确认</td>
  				<td><input type=password name=PassWordAgain></td>
  			</tr>
  			<tr>
  				<td colspan="2" align="center">
  					<input type="submit" οnclick="return register();" value="注册">
  					<input type="reset" value="重置">
  				</td>
			</tr>
  		</table>
  	</form>
  	<hr/>
  	当前时间:<%=(new java.util.Date()).toString()%>
  </body>
</html>



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值