欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

qqzone练习I

程序员文章站 2022-07-01 19:06:02
...

qqzone练习I

qqzone练习I

qqzone练习I

package com.cvs.contorller;

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

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

import com.cvs.dao.UserDao;
import com.cvs.model.User;

public class LoginServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public LoginServlet() {
		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 {

		String username=request.getParameter("username");
		String password=request.getParameter("password");
		UserDao dao=new UserDao();
		User user =dao.findUserByName();
		if (username.equals(user.getName())&&username.equals(user.getPassword())) {
			
			request.getRequestDispatcher("WebRoot/jsp/header.jsp").forward(request, response);
		} else {
			request.getRequestDispatcher("login.jsp").forward(request, response);


		}
		
		
		
	}

	/**
	 * 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 {
		doGet(request, response);
	}

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

}

package com.cvs.dao;



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

import com.cvs.model.User;
import com.cvs.util.DBUtil;

public class UserDao {
	public User findUserByName(){
		User user=new User(); 
		
		Connection conn= DBUtil.getConnection();
	try {
		PreparedStatement ps=conn.prepareStatement("select * from user where username=?");
		ps.setString(1, "username");
		ResultSet rs= ps.executeQuery();
		
		while (rs.next()) {

			String name= rs.getString("username");
			String password= rs.getString("password");
			user.setName(name);
			user.setPassword(password);
		}
		rs.close();
		ps.close();
		
	} catch (Exception e) {
		// TODO: handle exception
	}finally{
		DBUtil.closeConn();
		
		
	}
			
		
		
		return null;
	}
}

package com.cvs.model;

public class User {
		private int id;
		private String name;
		private String password;
		private String sex;
		private String birthday;
		private String hometown;
		public int getId() {
			return id;
		}
		public void setId(int id) {
			this.id = id;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public String getPassword() {
			return password;
		}
		public void setPassword(String password) {
			this.password = password;
		}
		public String getSex() {
			return sex;
		}
		public void setSex(String sex) {
			this.sex = sex;
		}
		public String getBirthday() {
			return birthday;
		}
		public void setBirthday(String birthday) {
			this.birthday = birthday;
		}
		public String getHometown() {
			return hometown;
		}
		public void setHometown(String hometown) {
			this.hometown = hometown;
		}
		@Override
		public String toString() {
			return "User [birthday=" + birthday + ", hometown=" + hometown
					+ ", id=" + id + ", name=" + name + ", password="
					+ password + ", sex=" + sex + ", getBirthday()="
					+ getBirthday() + ", getHometown()=" + getHometown()
					+ ", getId()=" + getId() + ", getName()=" + getName()
					+ ", getPassword()=" + getPassword() + ", getSex()="
					+ getSex() + ", getClass()=" + getClass() + ", hashCode()="
					+ hashCode() + ", toString()=" + super.toString() + "]";
		}
		

}

package com.cvs.util;


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

import com.cvs.model.User;

public class DBUtil {
	public static Connection conn=null;
	public static Connection getConnection() {
		try {
			Class.forName("com.mysql.jdbc.Driver");
			
		 conn= (Connection) DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/qqzone", "root", "123456");
		 
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return conn;
	}
	public static void closeConn(){
		if (null!=conn) {
			try {
				conn.close();
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();

			}
		}
	}
}

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>空间登录</title>
<link type="text/css" rel="stylesheet" href="static/css/login.css" />
<script type="text/javascript" src="static/js/jquery.js"></script>
<style type="text/css">
.form-actions .submit{
 cursor: pointer; 
 background: rgb(61, 157, 179);
 padding: 8px 5px;
 font-family: 'BebasNeueRegular','Arial Narrow',Arial,sans-serif;
 color: #fff;
 font-size: 20px; 
 margin: 5px;
 border: 1px solid rgb(28, 108, 122); 
 margin-bottom: 10px; 
 text-shadow: 0 1px 1px rgba(0, 0, 0, 0.5);
 border-radius: 3px; 
 box-shadow:0px 1px 6px 4px rgba(0, 0, 0, 0.07) inset,
 0px 0px 0px 3px rgb(254, 254, 254),
 0px 5px 3px 3px rgb(210, 210, 210);
 -webkit-transition: all 0.2s linear;
 transition: all 0.2s linear;
 }
 .form-actions .submit:hover{
 background: rgb(74, 179, 198);
 }
 .form-actions .submit:active,
 .form-actions .submit:focus{
 background: rgb(40, 137, 154);
 position: relative;
 top: 1px;
 border: 1px solid rgb(12, 76, 87); 
 box-shadow: 0px 1px 6px 4px rgba(0, 0, 0, 0.2) inset;
 }
</style>
</head>
<body>
	<div class="w">
		<div id="logo">
			<b></b>
		</div>
	</div>
	<form action="LoginServlet" method="post" cssClass="form-actions">
	
		<div class=" w1" id="entry">
			<div class="mc " id="bgDiv">
			<div id="logo">
			 <img src="static/img/logo.jpg" alt="空间" width="470" height="360" />
	    	</div>
				<div class="form ">
					<div class="item fore1">
						<span>用户名</span>
						<div class="item-ifo">
<%-- 						    <sf:input path="qq"/> --%>
							<input type="text" name="username"  class="text"  required="required"/>
							<div class="i-name ico"><sf:errors path="qq"></sf:errors></div>
							<label id="qq_error" class="hide"><b></b></label>
						</div>
					</div>

					<div class="item fore2">
						<span>密码</span>
						<div class="item-ifo">
<%-- 						    <sf:password path="password" cssClass="text" /><br> --%>
							<input type="password"  name="password" class="text" required="required" />
							<div class="i-pass ico"><sf:errors path="pwd"></sf:errors></div>
							<label id="loginpwd_error" class="hide"></label>
						</div>
					</div>
						<input type="submit" value="登陆" class="submit"/>
						<input type="reset" class="submit"/> 
					<pre>       ${login_fail}
					</pre>
				</div>
			</div>
			<div class="free-regist">
				<span><a href="regist.jsp" >免费注册&gt;&gt;</a></span>
			</div>
		</div>
	</form>
</body>
</html>
相关标签: java