您的位置:首页 > 数据库 > MySQL

Jsp+Servlet+Mysql测试案例

2017-08-09 21:04 429 查看
        对于初学者, 进行web项目开发 , 首先从jsp+servlet开始 , 下面介绍一个小型的  jsp+servlet+mysql  的项目案例!!!

      一. 首先创建一个动态的Web项目。

    

    在项目的WebContent的WEB-INF下放入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"
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">
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app> 
   二. 在该路径下导入两个jar包(mysql驱动包和servlet-api,可以百度进行下载 , )

  

  三. 在mysql中创建usertest表

CREATE TABLE users(
username VARCHAR(20) NOT NULL,
upassword VARCHAR(20) DEFAULT '123456'
);

INSERT INTO users VALUES('admin','admin');
--注意: 需要进行提交
commit;
 四. 在项目中加入JSP页面
   login.jsp

<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>
body{
margin:0 auto;
padding:0;
margin-top:150px;
text-align:center;
}
</style>
</head>
<body>
<form id="loginForm" method="POST" action="/Jsp_Servlet_Mysql/LoginServlet">
<input id="username" type="text" name="username" placeholder="请输入账号..."/><br/><br/>
<input id="password" type="password" name="password" placeholder="请输入密码..."/><br/><br/>
<input type="submit" value="登录"/>
</form>
</body>
</html>
success.jsp

<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<h2>欢迎您,登录成功...</h2>
</body>
</html>
五. 创建Servlet类

  LoginServlet.java 代码如下:

package com.lsy.servlet.web;

import java.io.IOException;
import java.sql.DriverManager;
import java.sql.ResultSet;

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

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

/**
* 加入@注解, 以此决定进入哪个servlet中进行出行数据
*/
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

public LoginServlet() {
super();
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}

@SuppressWarnings("unused")
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置字符编码集, 防止乱码 , 我这里是UTF-8
response.setContentType("text/heml;charset=UTF-8");
request.setCharacterEncoding("UTF-8");

Connection conn;
//在已经建立数据库连接的基础上,向数据库发送要执行的SQL语句。
Statement stmt = null;
ResultSet rs = null;

String message = "";
//这里根据注解 @WebServlet("/LoginServlet") 来取
//得login.jsp页面中name=“username”和name="upassword"==>>即用户名和密码
//注:页面中name中的名称与你数据库中的值一致
String username = request.getParameter("username");
String upassword = request.getParameter("upassword");

//登记JDBC驱动程序
try {
//注:mysql驱动包6.0以上版本。,驱动全类名 会发生变化,
Class.forName("com.mysql.jdbc.Driver");
System.out.print("数据库链接成功......");
} catch (Exception e) {
System.out.print("数据库链接失败!!!");
}

//这里根据
String url = "jdbc:mysql://localhost:3306/usertest";

try {
//链接数据库, 加入数据库用户名和密码
conn = (Connection) DriverManager.getConnection(url,"root","root");
//在已经建立数据库连接的基础上,向数据库发送要执行的SQL语句。用来创建不带参数的sql
stmt = (Statement) conn.createStatement();

String sql = "select * from users where username='"+username+"' and upassword= '"+upassword+"'";
//为Statement中一个方法,方法 executeQuery 用于产生单个结果集的语句,例如 SELECT 语句。
rs = stmt.executeQuery(sql);
System.out.println(rs.next());
if (rs !=null) { // 有数据库中存在该用户
//request.getSession() 取得session容器==>> HttpSession
request.getSession().setAttribute("username", username);
//关闭连接
rs.close();
stmt.close();
conn.close();
request.getSession().setAttribute("message","");
response.sendRedirect("/Jsp_Servlet_Mysql/page/success.jsp");
}else{
message = "用户名或密码有误";
request.getSession().setAttribute("message",message);
response.sendRedirect("/Jsp_Servlet_Mysq/page/login.jsp");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}


结果如下:

希望大家测试成功, 有问题可以留言共同讨论, 谢谢
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Jsp servlet 转发 学习