tomcat入门-web应用服务器搭建一个登陆功能

刚刚接触了web应用服务器,并尝试着用各个小功能做成一个提交在web应用服务器上的登陆功能。主要用到的是tomcat。该功能实现的结果就是把本地文件配置到web服务器上。



首先要进行基本的环境搭建,导入需要用到的jar包如下图所示,第一个是c3p0连接池包,第二个是jdbc工具dbutil工具,第三个是数据库连接包。

整体思路很简单,就是在数据库中存储用户的账号密码数据,用jdbc与数据库进行连接,并在服务器端用servlet做一个小程序,在前端放一个最最最简单的html文件就可以实现了。然后说说具体的步骤,在我的javaee中是这样一个层次,因为模块很简单,没有用到java的三层架构,如下图所示。


先把前端的控制界面写好,也就是上面右边的图,代码如下:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
	<title></title>
</head>
<body>
	<form action="/WEB13/login" method="post">
		用户名:<input type="test" name="username">
		密码:<input type="password" name="password">
		<input type="submit" name="登陆">
	</form>
</body>
</html>
注意:这个login.html文件的位置在WebContent目录下而不可以放在WEB—INF下,因为放在WEBINF下是属于服务器下一个不可访问的文件夹,这样的话是不可以访问到页面,会报404notfound。

另外还要配置好web.xml配置文件,这个文件可以在创建Servlet的时候创建好,如图:



mappings可以直接在这儿设置好路径,之后自己需要设置的设置一下,这是我的web配置文件:

<?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_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>WEB13</display-name>
  <servlet>
    <servlet-name>abc</servlet-name>
    <servlet-class>com.liujie.QuickServlet2</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>abc</servlet-name>
    <url-pattern>/quickservlet</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>
  <servlet>
    <description></description>
    <display-name>QuickServlet2</display-name>
    <servlet-name>QuickServlet2</servlet-name>
    <servlet-class>com.liujie.QuickServlet2</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>QuickServlet2</servlet-name>
    <url-pattern>/quickServlet2</url-pattern>
  </servlet-mapping>
  <servlet>
    <description></description>
    <display-name>LoginServlet</display-name>
    <servlet-name>LoginServlet</servlet-name>
    <servlet-class>com.login.LoginServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>LoginServlet</servlet-name>
    <url-pattern>/login</url-pattern>
  </servlet-mapping>
</web-app>
下面就直接些整体代码,其实也就三块,还有一块只是个封装javabean对象用的,所以只有两个。

1.LoginServlet.java:

package com.login;

import java.io.IOException;
import java.sql.SQLException;

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

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import com.damain.User;
import com.utils.DataSourceUtils;


public class LoginServlet extends HttpServlet {

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//1.获得用户名和密码
		String username=request.getParameter("username");
		String password=request.getParameter("password");
		
		//2.从数据库中验证用户名和密码
		QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
		String sql="select * from user where username=? and password=?";
		User user=null;
		try {
			user=qr.query(sql, new BeanHandler<User>(User.class),username,password);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		//3.根据返回的结果给用户
		if(user!=null)
		{
			response.getWriter().write(user.toString());
		}
		else
		{
			response.getWriter().write("username or password is wrong!");
		}
	}

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

}

2.DataSourceUtils.java

package com.utils;

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

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class DataSourceUtils {

	private static DataSource dataSource = new ComboPooledDataSource();

	private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>();

	// 直接可以获取一个连接池
	public static DataSource getDataSource() {
		return dataSource;
	}

	// 获取连接对象
	public static Connection getConnection() throws SQLException {

		Connection con = tl.get();
		if (con == null) {
			con = dataSource.getConnection();
			tl.set(con);
		}
		return con;
	}

	// 开启事务
	public static void startTransaction() throws SQLException {
		Connection con = getConnection();
		if (con != null) {
			con.setAutoCommit(false);
		}
	}

	// 事务回滚
	public static void rollback() throws SQLException {
		Connection con = getConnection();
		if (con != null) {
			con.rollback();
		}
	}

	// 提交并且 关闭资源及从ThreadLocall中释放
	public static void commitAndRelease() throws SQLException {
		Connection con = getConnection();
		if (con != null) {
			con.commit(); // 事务提交
			con.close();// 关闭资源
			tl.remove();// 从线程绑定中移除
		}
	}

	// 关闭资源方法
	public static void closeConnection() throws SQLException {
		Connection con = getConnection();
		if (con != null) {
			con.close();
		}
	}

	public static void closeStatement(Statement st) throws SQLException {
		if (st != null) {
			st.close();
		}
	}

	public static void closeResultSet(ResultSet rs) throws SQLException {
		if (rs != null) {
			rs.close();
		}
	}

}
3.User.java

package com.damain;

public class User {

	private int id;
	private String username;
	private String password;
	private String email;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	
	
}
另外,为了和连接池建立连接,当然还要有c3p0的配置文件,否则会无法连接到数据库资源报404。并会在控制台抛出异常,这种异常是需要一点时间的,因为在访问数据库一段时间内还没有找到资源才会抛出异常。

下面是我的c3p0配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>

  <default-config>
	<property name="user">root</property>
	<property name="password">123</property>
    <property name="driverClass">com.mysql.jdbc.Driver</property>
	<property name="jdbcUrl">jdbc:mysql:///mybase</property>
  </default-config>

</c3p0-config>

再给出我的数据库信息:


最后在地址栏输入:,出现登陆界面

而如果只是输入

则404。这是因为在外层docs里面的WEBINF实现了web.xml文件的配置,而在子文件下的WEBINF下的xml会覆盖外面的web.xml,又因为

当你访问资源地址所有的servlet都不匹配时 , 缺省的servlet负责处理,因此由当前目录下的xml配置文件处理,会转到当前的servlet,但是静态文件并不在当前目录下,因此会报找不到资源错误。

如果用户名密码正确则输出如果错误则输出

这样一个小功能就实现了,虽然很少,但是自己第一次做出来还是要点时间,刚开始学,有错误欢迎指出。



  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

没想好叫什么名字

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值