WebProject

UserBean.java:Class

package com.neusoft.beans;

public class UserBean {

	private String id;
	private String name;
	private String password;

	public String getId() {

		return id;
	}

	public void setId(String 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;
	}

}

LoginDao.java:Class

package com.neusoft.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.neusoft.beans.UserBean;
import com.neusoft.utils.DbCon;

public class LoginDao {

	public UserBean Login(String name, String password) {
		Connection con = DbCon.con();
		ResultSet rs = null;
		UserBean ub = null;
		try {
			Statement st = con.createStatement();
			String sql = "select * from USERTEST where username='"
				+ name + "' and password = " + password;
			System.out.println(sql);
			rs = st.executeQuery(sql);
			while (rs.next()) {
				ub = new UserBean();
				ub.setId(rs.getString("id"));
				ub.setName(rs.getString("username"));
				ub.setPassword(rs.getString("password"));
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return ub;
	}
}


LoginServlet.java:Servlet

package com.neusoft.services;

import java.io.IOException;

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.neusoft.beans.UserBean;
import com.neusoft.dao.LoginDao;

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 {

		// 可以解决post提交方式,get搞不定
		// request.setCharacterEncoding("gbk");

		// 单纯的写入java业务逻辑代码就可以
		// this.doPost(request, response);
		System.out.println("我经过了servlet跳转");
		// 验证用户名和密码的合法性
		UserBean ub = null;
		LoginDao l = new LoginDao();
		// 转码
		String name = new String(request.getParameter("username").getBytes(
				"iso-8859-1"), "gbk");
		// String name = request.getParameter("username");
		String password = request.getParameter("password");
		System.out.println(name);
		System.out.println(password);
		ub = l.Login(name, password);
		if (ub != null) {
			HttpSession se = request.getSession();
			se.setAttribute("ub", ub);
			response.sendRedirect("../main/frame_set.jsp");
		}else{
			response.sendRedirect("../index.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 {
		// 单纯的写入java业务逻辑代码就可以
		this.doGet(request, response);
	}

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

}

DbCon.java:类

package com.neusoft.utils;

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

public class DbCon {

	public static Connection con() {
		String url = "jdbc:oracle:thin:@10.25.85.247:1521:orcl";
		String user = "scott";
		String password = "tiger";
		Connection con = null;
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			con = DriverManager.getConnection(url, user, password);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return con;
	}
	
	public static void close(Connection con ,Statement st ,ResultSet rs){
		try {
			if (rs != null) {
				rs.close();
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			if (st != null) {
				st.close();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		try {
			if (con != null) {
				con.close();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void close(Connection con ,Statement st){
		
		try {
			if (st != null) {
				st.close();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		try {
			if (con != null) {
				con.close();
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}


Login.jsp:放在:Login文件夹下

<%@ page language="java" contentType="text/html; charset=GB18030"
	pageEncoding="GB18030"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
		<title>Insert title here</title>
		<script type="text/javascript">
	function test() {
		var name_1 = document.formData.password.value;
		if(name_1.length<6){
		alert("密码不足六位,重新输入");
		return false;
		}
		return true;
	}
</script>

	</head>
	<body>
		<form action="../servlet/LoginServlet" method="post" name="formData">
			用户名:
			<input type="text" name="username" value="neusoft" />
			<br>
			密码:  
			<input type="password" name="password" value="123456">
			<br>
			<input type="submit" value="提交" onclick="return test()">
			<input type="reset" value="重置">

		</form>
	</body>
</html>

frame_a.jsp:和下frame前缀的都放在main文件夹下

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
	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 '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">
		<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	</head>

	<body>
		欢迎<font color="red">${ub.name}</font>登录本系统.
	</body>
</html>


frame_b.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
	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 '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">
		<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	</head>

	<body>
		<a href="../index.jsp" target="main">登录</a>
		<br />
		<a href="url">增加</a>
		<br />
		<a href="url">删除</a>
		<br />
		<a href="url">修改</a>
		<br />

	</body>
</html>


frame_c.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
	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 '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">
		<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	</head>

	<body>
		

	</body>
</html>


frame_set.jsp:

<html>
	<head></head>
	<frameset rows="20%,80%">
		<frame name="top" src="frame_a.jsp" framef="0">
		<frameset cols="20%,80%">
			<frame name="left" src="frame_b.jsp" scrolling=no noresize
				framespacing="0">
			<frame name="main" src="frame_c.jsp" bordercolor="red"
				marginwidth="100" marginheight="50" framespacing="0">
		</frameset>
		<body></body>
		</noframes>
	</frameset>
</html>


index.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
	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 '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">
		<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	</head>

	<body>
		用户名或密码不存在.<a href="Login/Login.jsp">点我重新登录</a>
		<br>
	</body>
</html>


web.xml:

  <?xml version="1.0" encoding="UTF-8" ?> 
- <web-app version="2.5" 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_2_5.xsd">
- <servlet>
  <servlet-name>LoginServlet</servlet-name> 
  <servlet-class>com.neusoft.services.LoginServlet</servlet-class> 
  </servlet>
- <servlet-mapping>
  <servlet-name>LoginServlet</servlet-name> 
  <url-pattern>/servlet/LoginServlet</url-pattern> 
  </servlet-mapping>
- <welcome-file-list>
  <welcome-file>index.jsp</welcome-file> 
  </welcome-file-list>
  </web-app>



可能有个错误

你可能感兴趣的:(WebProject)