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;
		
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值