web网页Eclipse,jsp+Servlet+javaBean,访问Mysql链接数据库

注意导完驱动包,还要在WEB-INF/lib下再放一份。

LogIn.jsp代码

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
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 'LogIn.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>
  	<!-- <form method="post" action="Check.jsp"> -->
  	<form method="post" action="/servletStudy_20160302/ServletDemo">
  		用户名:<input type="text" size="20" name="name"><br>
  		密        码:<input type="password" size="20" name="password">
  		<input type="submit" value="登录" name="Submit"><br>
  	</form>
  </body>
</html>

注意 action的“/servletStudy_20160302/ServletDemo”前面是项目名称后面是servlet类

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>servletStudy_20160302</display-name>
  <servlet>
    <servlet-name>ServletDemo</servlet-name>
    <servlet-class>tju.chc.test.ServletDemo</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>ServletDemo</servlet-name>
    <url-pattern>/ServletDemo</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

还有一点,

下面是ServletDemo中的doPost方法

public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		response.setCharacterEncoding("utf8");//这一句是为防止乱码
		PrintWriter out = response.getWriter();//
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE><meta charset=\"gbk\"></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the POST method");
		DatabaseBean databaseBean = new DatabaseBean();
		String userName = request.getParameter("name");
		String userPS = request.getParameter("password");
		try{
			if (databaseBean.logInUser(userName, userPS)) {
				out.println(userName + ",sdfasdf你已经登陆成功!");
			}
		}catch(Exception e){
			out.println("输入的用户名或者密码有误!");
		}
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

DatabaseBean.java

package tju.chc.database;

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

public class DatabaseBean {
	private Connection connection = null;
	private Statement statement = null;
	private ResultSet resultSet = null;
	
	public DatabaseBean(){
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mytest1", "root", "");
			statement = connection.createStatement();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	public boolean logInUser(String userName,String userPassword){
		boolean userExit = false;
		String strSql = "select * from user where userId = '"+userName+"' and uPS = '" +userPassword+ "'";
		try {
			resultSet = statement.executeQuery(strSql);
			if(resultSet.next()){
				String userNam = resultSet.getString("userId");
				String userPS = resultSet.getString("uPS");
				System.out.println(userNam+","+userPS);
				userExit = true;
				connection.close();
				return userExit;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			try {
				connection.close();
				return false;
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		return userExit;
		
	}
}



你可能感兴趣的:(java,eclipse,mysql,jsp,Web)