• 微信公众号:美女很有趣。 工作之余,放松一下,关注即送10G+美女照片!

第八周JSP作业

开发技术 开发技术 2个月前 (04-28) 23次浏览

一,

要求 邮件系统 用分层实现 登录 注册  显示邮件列表 3个功能

1,users表

第八周JSP作业

2,msg表

第八周JSP作业

3,项目包第八周JSP作业

4,bean类(Users.java

package com.gd.bean;

public class Users {
	private String username;
	private String password;
	private String eamil;
	
	public Users(){}
	
	public Users(String username,String password,String eamil){
		
		this.username=username;
		this.password = password;
		this.eamil = eamil;
	}
	
	
	public String getEamil() {
		return eamil;
	}

	public void setEamil(String eamil) {
		this.eamil = eamil;
	}

	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	

}

  bean(Msg。java)

package com.gd.bean;

public class Msg {
	
	private int msgid;
	private String username;
	private String title;
	private String msgcontent;
	private String state;
	private String sendto;
	private String msg_create_date;
	public int getMsgid() {
		return msgid;
	}
	public void setMsgid(int msgid) {
		this.msgid = msgid;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getMsgcontent() {
		return msgcontent;
	}
	public void setMsgcontent(String msgcontent) {
		this.msgcontent = msgcontent;
	}
	public String getState() {
		return state;
	}
	public void setState(String state) {
		this.state = state;
	}
	public String getSendto() {
		return sendto;
	}
	public void setSendto(String sendto) {
		this.sendto = sendto;
	}
	public String getMsg_create_date() {
		return msg_create_date;
	}
	public void setMsg_create_date(String msg_create_date) {
		this.msg_create_date = msg_create_date;
	}
	
	
	
	

}

  5,dao(BaseDao.java)—–链接MySQL

package com.gd.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class BaseDao {	
	//获取连接
	protected Connection getConnection(){
		Connection conn=null;
			try {
				Class.forName("com.mysql.jdbc.Driver");
				// 2.建立连接
				conn = DriverManager.getConnection(
						"jdbc:mysql://localhost:3306/mysql", "root", "root");
			} catch (Exception e) {
				e.printStackTrace();
			} 
			return conn;
	}	
	
	
	//关闭连接
	protected void closeAll(Connection con,PreparedStatement ps,ResultSet rs){		
	try {
		if(rs != null)
			rs.close();
		if(ps != null)
			ps.close();
		if(con != null)
			con.close();
		
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}

  Msg.java(查询邮件)

package com.gd.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.gd.bean.Msg;


public class MsgDao extends BaseDao{
	
	//查询邮件列表方法
	public List<Msg> getReceive(String uname){
		List<Msg> list = new ArrayList<Msg>();
		Connection conn = getConnection();
		
		String sql = "select * from msg where sendto=?";	
		
		try {
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, uname);
			ResultSet rs = ps.executeQuery();
			while(rs.next()){
				Msg msg = new Msg();
				msg.setUsername(rs.getString("username"));
				msg.setTitle(rs.getString("title"));
				msg.setMsgcontent(rs.getString("msgcontent"));
				msg.setState(rs.getString("state"));
				msg.setSendto(rs.getString("sendto"));
				msg.setMsg_create_date(rs.getString("msg_create_date"));
				list.add(msg);
			}
			closeAll(conn, ps, rs);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return list;	
	}

}

  

UsersDao.java( 登录,注册)

package com.gd.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.gd.bean.Msg;
import com.gd.bean.Users;
import com.sun.xml.internal.bind.v2.schemagen.xmlschema.List;

public class UsersDao extends BaseDao{
	
	private static final PreparedStatement preparedStatement = null;

	//登录
	public boolean login(String username,String password){
		boolean f = false;
		Connection conn = getConnection();
		
		String sql = "select * from users where username=? and password =?";	
		
		try {
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, username);
			ps.setString(2, password);
			ResultSet rs = ps.executeQuery();
			while(rs.next()){
				f=true;
			}
			
			closeAll(conn, ps, rs);
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		return f;
	}
	
	//注册
	public int adduser(Users us){
		int q= 0;
		Connection connection =getConnection();
		String sqlString = "insert into users values(?,?,?)";
		
		try {
			PreparedStatement preparedStatement = connection.prepareStatement(sqlString);
			preparedStatement.setString(1, us.getUsername());
			preparedStatement.setString(2, us.getPassword());
			preparedStatement.setString(3, us.getEamil());
			
			q = preparedStatement.executeUpdate();
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			closeAll(connection,preparedStatement, null);
		}
		
		
		return q;
	}
	
	
	
	

}

  6,界面

login.jsp(登录)

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>CSS控制表单样式</title>
<style type="text/css">
body{ font-size:12px; font-family:"宋体";}                /*全局控制*/
body,form,input,p{ padding:0; margin:0; border:0;}       /*重置浏览器的默认样式*/
form{
	width:320px;
	height:150px;
	padding-top:20px;
	margin:50px auto;                      /*使表单在浏览器中居中*/
	background:#f5f8fd;                    /*为表单添加背景颜色*/
	border-radius:20px;                    /*设置圆角边框*/ 
	border:3px solid #4faccb;
} 
p{
	margin-top:15px; 
	text-align:center;
}                   
p span{
	width:40px;
	display:inline-block; 
	text-align:right;
	}                 
.num,.pass{                                   /*对文本框设置共同的宽、高、边框、内边距*/ 
	width:152px;                                      
	height:18px;                                 
	border:1px solid #38a1bf;
    padding:2px 2px 2px 22px; 
} 
.num{                                         /*定义第一个文本框的背景、文本颜色*/
	 background:url(images/1.jpg) no-repeat 5px center #FFF;
	 color:#999;
}                     
.pass{                                        /*定义第二个文本框的背景*/                                                    
	 background:url(images/2.jpg) no-repeat 5px center #FFF;
}
.btn01,.btn02{
	width:60px; 
	height:25px; 
	border-radius:3px;                        /*设置圆角边框*/ 
	border:1px solid #6b5d50; 
	margin-left:30px;
	}
.btn01{ background:#3bb7ea;}                  /*设置第一个按钮的背景色*/   
.btn02{ background:#fb8c16;}                  /*设置第二个按钮的背景色*/                        
</style>
</head>
<body>
<form action="dologin.jsp" method="post">	
    <p>
        <span>账号:</span>
        <input type="text" name="uname" value="admin" class="num"  />
    </p>
    <p>
         <span>密码:</span>
         <input type="password" name="upass" class="pass"/>
    </p>
    <p>
        <input type="submit" class="btn01" value="登录"/>
        <a href="register.jsp">没有账户?点击注册</a>
   </p>
</form>
</body>
</html>

  dologin.jsp( 登录处理逻辑)

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

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'dologin.jsp' starting page</title>
    
	
  </head>
  
  <body>
    <% 
    request.setCharacterEncoding("utf-8");
    
    String name = request.getParameter("uname"); 
    String pass = request.getParameter("upass"); 
     
    UsersDao ud = new UsersDao();
    if(ud.login(name, pass)){ 
    	session.setAttribute("name", name);
    	response.sendRedirect("main.jsp");
    	
    }else{
    	response.sendRedirect("login.jsp");
    }
     
     
     %>
    
    
    
    
  </body>
</html>

  register.jsp(注册)

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>CSS控制表单样式</title>
<style type="text/css">
body{ font-size:12px; font-family:"宋体";}                /*全局控制*/
body,form,input,p{ padding:0; margin:0; border:0;}       /*重置浏览器的默认样式*/
form{
	width:320px;
	height:150px;
	padding-top:20px;
	margin:50px auto;                      /*使表单在浏览器中居中*/
	background:#f5f8fd;                    /*为表单添加背景颜色*/
	border-radius:20px;                    /*设置圆角边框*/ 
	border:3px solid #4faccb;
} 
p{
	margin-top:15px; 
	text-align:center;
}                   
p span{
	width:40px;
	display:inline-block; 
	text-align:right;
	}                 
.num,.pass{                                   /*对文本框设置共同的宽、高、边框、内边距*/ 
	width:152px;                                      
	height:18px;                                 
	border:1px solid #38a1bf;
    padding:2px 2px 2px 22px; 
} 
.num{                                         /*定义第一个文本框的背景、文本颜色*/
	 background:url(images/1.jpg) no-repeat 5px center #FFF;
	 color:#999;
}                     
.pass{                                        /*定义第二个文本框的背景*/                                                    
	 background:url(images/2.jpg) no-repeat 5px center #FFF;
}
.btn01,.btn02{
	width:60px; 
	height:25px; 
	border-radius:3px;                        /*设置圆角边框*/ 
	border:1px solid #6b5d50; 
	margin-left:30px;
	}
.btn01{ background:#3bb7ea;}                  /*设置第一个按钮的背景色*/   
.btn02{ background:#fb8c16;}                  /*设置第二个按钮的背景色*/                        
</style>
</head>
<body>
<form action="doreg.jsp" method="post">	
    <p>
        <span>账号:</span>
        <input type="text" name="uname" value="admin" class="num"  />
    </p>
    <p>
         <span>密码:</span>
         <input type="password" name="upass" class="pass"/>
    </p>
    <p>
         <span>邮箱:</span>
         <input type="email" name="uemail" class="pass"/>
    </p>
    <p>
        <input type="submit" class="btn01" value="注册"/>   
        <a href="login.jsp">注册完成返回登录</a>   
   </p>
</form>
</body>
</html>

  doreg.jsp(注册实现)

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="com.gd.dao.*"%>
<%@ page import="com.gd.bean.*"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title></title>
    
  </head>
  
  <body>
  <%
   	request.setCharacterEncoding("utf-8");
    
    String name = request.getParameter("uname"); 
    String pass = request.getParameter("upass"); 
    String email = request.getParameter("uemail"); 
  
  	
  	Users us = new Users();
  	us.setUsername(name);
  	us.setPassword(pass);
  	us.setEamil(email);
  	
  	UsersDao ud = new UsersDao();
  	if( ud.adduser(us) !=0){
  	%>
  	<script type="text/javascript">
  		alert("注册成功");
  	</script>
  	<% 	
  	response.sendRedirect("login.jsp");
  	}else{
  	%>
  	<script type="text/javascript">
  		alert("失败");
  	</script>
  	
  	<%
  	response.sendRedirect("doreg.jsp"); 
  	}
   %>
  

  </body>
</html>

  

main.jsp(邮件查询)

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ page import="com.gd.dao.*"%>
<%@ page import="com.gd.bean.*"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title></title>
   
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
  
    <%
    String name=(String)session.getAttribute("name");
    out.print("欢迎用户"+name+":");
    
    MsgDao msgdao = new MsgDao();
    Msg msg = new Msg();
    
    List<Msg> list = new ArrayList<Msg>();
    list = msgdao.getReceive(name);
   	Iterator<Msg> it = list.iterator();  
   	
   	while(it.hasNext()){
   		msg = it.next();
   		
   		%>
   		<p>发件人----------标题----------内容----------状态----------收件人----------时间
   		<hr>
   		
   		<%
   		out.print(msg.getUsername()+"----------"+msg.getTitle()+"----------"+msg.getMsgcontent()+"----------"
   		+msg.getState()+"----------"+msg.getSendto()+"----------"+msg.getMsg_create_date());  
   		}
   		%>
   	
    
     
    
    
    
    
    
    
  </body>
</html>

  第八周JSP作业第八周JSP作业

第八周JSP作业


程序员灯塔
转载请注明原文链接:第八周JSP作业
喜欢 (0)