binguan宾馆:MVC:jsp+servlet+javabean

MVC的全名是Model View Controller,是模型(Model)-视图(view)-控制器(controller)的缩写,是一种设计模式。
jsp动态网页

register.html

<html><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312"></head><body>

<form action="/binguan/register" method="get">
<table><tr><td>name:<input type="text" name="userName"></td></tr><tr><td>key:<input type="text" name="key"></td></tr><tr><td>phone:<input type="text" name="phone"></td></tr><tr><td>gender:<input type="text" name="gender"></td></tr><tr><td>
<input type="submit" name="action" value="view"><input type="submit" name="action" value="add">
</td></tr></table></form></html>
<servlet>
        <servlet-name>RegisterServlet</servlet-name>
        <servlet-class>javaee.ch7.RegisterServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>RegisterServlet</servlet-name>
        <url-pattern>/register</url-pattern>
    </servlet-mapping>

C:\Users\李白\Desktop\新建文件夹\binguan\src\javaee\ch7

package javaee.ch7;import javax.servlet.*;import javax.servlet.http.*;import java.sql.*;import java.util.*;import java.io.*;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;
public class RegisterServlet extends HttpServlet
{   Connection con;

	public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException
    {
        request.setCharacterEncoding("gb2312");response.setContentType("text/html;charset=gb2312"); PrintWriter out = response.getWriter();String action=(String)request.getParameter("action");String userName=(String)request.getParameter("userName"); String key=(String)request.getParameter("key");String gender=(String)request.getParameter("gender");String phone=(String)request.getParameter("phone");
        if(action.equals("add")){/*if(aaa==null||aaa=="") {try{deleteContact(userName,key,gender,phone);out.println("成功!"+userName);}catch(Exception e){out.println("失败或者没有这个信息:"+userName);}}else {out.println("already have one!"+userName);}*/    	}
        else if(action.equals("view")){ResultSet rst=getContact(userName);out.println("<table border=1><tr><td>already</td></tr>");try{while(rst.next()){out.println("<tr>");out.println("<td>"+rst.getString("name")+"</td>"); /*out.println("<td>"+rst.getInt("key")+"</td>");out.println("<td>"+rst.getString("phone")+"</td>");*/out.println("</tr>");}out.println("</table>");rst.close();}catch(Exception e){}}    
        else if(action.equals("login")) 
        {  HttpSession session= request.getSession(); // 获取(创建)Session对象;
        	try {
        		ResultSet rest=getContact(userName);
        		rest.next();
        		if( userName.equals(rest.getString("name"))&&key.equals(rest.getString("key"))   )
        		{out.println("caozuo");
        		out.println("cunrusession");
        		
        		 session.setAttribute("username",rest.getString("name") );//用session保存
        		 out.println("cunrusessionok");
        		 request.getRequestDispatcher("yonghuchaxun.html").forward(request, response);
        		}
        		else {out.println("wrong!");}
        		
        	}catch(Exception e) {
        		out.println("shazheshishaaaaaaaaaaaa");
        	}
        }
        else if(action.equals("change")) 
            { 
            }
        
        
   	}


	public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException
	 {doGet(request,response);}
	
	//创建一个Connection对象,用于执行数据库操作。    
    public   RegisterServlet()
    {
    	/*
    	String CLASSFORNAME="com.mysql.jdbc.Driver";String SERVANDDB="jdbc:mysql://127.0.0.1:3306/binguan?serverTimezone=UTC";String USER="root";
	    String PWD="123456";  try
	    {Class.forName(CLASSFORNAME);con = DriverManager.getConnection(SERVANDDB,USER,PWD);}catch(Exception e){e.printStackTrace();}*/
    	this.con=DataBaseConnection.getConnection();
	}
	
		//在数据库中添加信息
	public void deleteContact(String userName,String key,String gender, String phone)throws Exception
	{	
		try
		{
			PreparedStatement pstmt=con.prepareStatement("insert into user values(?,?,?,?,?,?)");
			pstmt.setString(1,userName);
		    pstmt.setString(2,key);
		    pstmt.setString(3,phone);
		    pstmt.setString(4,"100");
		    pstmt.setString(5,gender);
		    pstmt.setString(6,"100");
	        pstmt.execute();			
		}
		catch(Exception e)
		{
			e.printStackTrace();
			throw e;
		}
}
		//查询数据库
	public  ResultSet getContact(String userName)
	{
		try
		{
			Statement stm=con.createStatement();
			ResultSet result=stm.executeQuery("select * from user where name='"+userName+"'");
			return result;
		}
		catch(Exception e)
		{
		}
		return null;
	}	
	
}

useBeanguanlishouye.jsp(管理订单首页)

<%@ page contentType="text/html; charset=gb2312" language="java"
	import="java.sql.*,java.io.*"%>
<jsp:useBean id="contact" class="javaee.ch7.ContactBean" scope="page" />

<html><body>
	<a href="contact_servlet_test.html" >用户查询与管理</a>  <a href="guanli.jsp" >用户管理</a>
    <a href="fangjianxinxitainxie.html" >fangjian管理</a> <a href="guanlidingdan.jsp" >dingdan管理</a></br></br></br></br>
  用户独立的私密信息(使用JavaBean来操作数据库):<hr>
	<table border=1>
		<tr><td>姓名</td><td>手机</td><td>ID</td><td>email</td><td>最后的login时间</td><td>others</td>
		</tr>
		<%
			try {
				contact.addContact();
				ResultSet rst = contact.getContact("hellking");
while (rst.next()) {out.println("<tr>");out.println("<td>" + rst.getString("userName") + "</td>");out.println("<td>" + rst.getInt("mobile") + "</td>");out.println("<td>" + rst.getString("phone") + "</td>");out.println("<td>" + rst.getString("mail") + "</td>");out.println("<td>" + rst.getDate("lastcontact") + "</td>");out.println("<td>" + rst.getString("mem") + "</td>");out.println("</tr>");}rst.close();
			} catch (Exception e) {}
		%>
	</table>
</body>
</html>

ContactBean.java

package javaee.ch7;
import java.sql.*;

public class ContactBean
{
	private Connection con;
	//初始化连接。
	public ContactBean()
	{
	 	String CLASSFORNAME="com.mysql.cj.jdbc.Driver";
	    String SERVANDDB="jdbc:mysql://127.0.0.1:3306/binguan?serverTimezone=UTC";
	    String USER="root";
	    String PWD="123456"; 
	    try
	    {
	    	
	    	Class.forName(CLASSFORNAME);
			con = DriverManager.getConnection(SERVANDDB,USER,PWD);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	//在数据库中添加信息
	public void addContact()throws Exception
	{
		try
		{
			PreparedStatement pstmt=con.prepareStatement("insert into Contact values(?,?,?,?,?,?)");
			pstmt.setString(1,"hellking");
		    pstmt.setInt(2,136234234);
		    pstmt.setString(3,"010455554");
		    pstmt.setString(4,"lxh@some.com");
		    pstmt.setDate(5,new java.sql.Date(new java.util.Date().getTime()));
		    pstmt.setString(6,"other email:ddd@d.com");
	        pstmt.execute();	
	        pstmt.close();
			//con.close();
		}
		catch(Exception e)
		{
			e.printStackTrace();
			throw e;
		}
	}
	//查询数据库
	public  ResultSet getContact(String userName)
	{
		try
		{
			Statement stm=con.createStatement();
			
			ResultSet result=stm.executeQuery("select * from contact");
			result.close();
			//con.close();
			return result;
		}
		catch(Exception e)
		{
		}
		return null;
	}
}

binguan宾馆:MVC:jsp+servlet+javabean

上一篇:LockSupport


下一篇:Exchanger