servlet 生命周期及doget(),dopost()实现简单的登录注册功能

servlet生命周期

servlet生命周期分为 init()初始化,service()服务,destroy()销毁。
生命周期
在web.xml里面配置
在这里插入图片描述

无论servlet服务启动多少次,init()初始化只运行一次
在这里插入图片描述
可以通过 getRemoteAddr() 方法获取访问主机的IP地址

myeclipse 创建项目时有可能会出现下面错误,这是由于java.jdk版本问题可以通过右键项目–>build path -->configure build path -->java build path–>libraries ,再按照下面的步骤修改成将jdk1.6即可
在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

doget() dopost()

在这里插入图片描述
get和post都能提交数据,通过get提交的数据有大小的限制,post提交数据没有数据大小的限制

在这里插入图片描述
在表单中可选择提交数据的方式(get或post),使用get方法地址栏中显示传输的内容,使用post方法则不会显示

请添加图片描述
请添加图片描述

注册登陆界面

登录

package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

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

public class LoginServlet extends HttpServlet {

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

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

		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");

		String pwd = request.getParameter("pwd");
		String userName = request.getParameter("userName");
		ResultSet rs = null;
		try {
			Connection conn = DBUtil.getConn();

			String sql = "select * from users where username = ? and pwd = ?";

			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, userName);
			ps.setString(2, pwd);

			rs = ps.executeQuery();

			

		} catch (SQLException e) {
			e.printStackTrace();
		}

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		try {
			if (rs.next()) {
				out.print("登录成功!");
			} else {
				out.print("登录失败!");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		out.flush();
		out.close();
	}

}

在这里插入图片描述

注册

package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

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

import yanglinchao.util.DBUtil;

public class RegServlet extends HttpServlet {

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

		doPost(request, response);
	}

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

		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");

		String name = request.getParameter("name");
		String pwd = request.getParameter("pwd");
		String userName = request.getParameter("userName");
		String ageStr = request.getParameter("age");
		Integer age = Integer.parseInt(ageStr);

		boolean isTrue = false;
		try {
			Connection conn = DBUtil.getConn();

			String sql = "INSERT INTO users(name,username,pwd,age) VALUES (?,?,?,?)";

			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, name);
			ps.setString(2, userName);
			ps.setString(3, pwd);
			ps.setInt(4, age);

			int count = ps.executeUpdate();

			if (count > 0) {
				isTrue = true;
			} else {
				isTrue = false;
			}

		} catch (SQLException e) {
			e.printStackTrace();
		}

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		if (isTrue == true) {
			out.print("注册成功!");
		} else {
			out.print("注册失败!");
		}

		out.flush();
		out.close();
	}

}

在这里插入图片描述
简单的一个跳转注册登陆界面!

©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页