JavaWeb_day17_Request__请求中文乱码__请求转发__BeanUtils__用户登录错误回显

HTTP请求包括

请求行   包括请求方式,资源地址,协议版本

请求头  请求头是客户端发送给服务器端的一些信息,使用键值对表示key:value

请求体  当请求方式是post的时,请求体会有请求的参数,格式如下:

username=zhangsan&password=123

如果请求方式为get,那么请求参数不会出现在请求体中,会拼接在url地址后面

1.通过request获得请求行

获得客户端的请求方式:request.getMethod();   //返回的是get或者post

获取web应用的名称: request.getContextPath();  //返回的是/项目名称,/day18

 获得访问的客户端IP地址:request.getRemoteAddr();

get请求提交url地址后的参数字符串:request.getQueryString(); //返回的是username=zs&password=11,如果是post请求返回的是null

2.通过request获得请求头

获取指定头字段的值:request.getHeader(String str); //例:request.getHeader("User-Agent");

3.获取用户请求的参数和解决出现的中文乱码问题

方法:

demo:

request中的乱码:

post:  request.setCharacterEncoding(“UTF-8”);   //但表格提交大部分都是使用post方法

get:  parameter = new String(parameter.getBytes(“iso8859-1”),”UTF-8”); //先将参数编码成iso8859-1,再解码成UTF-8

 

4.重定向(xx.sendRedirect)和请求转发(xx.getRequestDispatcher().forward())的区别

1)重定向请求两次,转发请求一次

2)重定向地址发生改变,转发地址不改变

3)重定向可以访问外部网站,转发只能访问内部资源

4)转发的性能优于重定向

servlet1:

servlet2:

 

request.getRequestDispatcher(转发的地址).forward(req,resp);

request.setAttribute(name,value)

request.getAttribute(name)

 

5.ServletContext域和request域的生命周期比较:

ServletContext:创建:服务器启动

                             销毁:服务器关闭

                              域的作用范围:整个web应用

request:             创建:访问是创建request

                             销毁:响应结束request销毁

                              域的作用范围:一次请求中

6.客户端地址和服务器端地址的写法:

客户端地址:是客户端去访问服务器的地址,服务器外部的地址。特点是:写上web应用的名称

服务器端地址:服务器端内部资源的跳转的地址,特点:不需要写web应用的名称

 

7.BeanUtils的使用

BeanUtils工作原理:将map中的数据,根据key与实体的属性的对应关系封装,只要key的名字与实体的属性的名字一样,就自动封装到实体中

demo:将页面用户注册的表单信息存到数据库中

第一步:需要使用到的库导入

第二步:在WebContent中创建页面

register.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head></head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>会员注册</title>
<link rel="stylesheet" href="css/bootstrap.min.css" type="text/css" />
<script src="js/jquery-1.11.3.min.js" type="text/javascript"></script>
<script src="js/bootstrap.min.js" type="text/javascript"></script>
<!-- 引入自定义css文件 style.css -->
<link rel="stylesheet" href="css/style.css" type="text/css" />

<style>
body {
	margin-top: 20px;
	margin: 0 auto;
}

.carousel-inner .item img {
	width: 100%;
	height: 300px;
}

font {
	color: #3164af;
	font-size: 18px;
	font-weight: normal;
	padding: 0 10px;
}
</style>
</head>
<body>

	<!-- 引入header.jsp -->
	<jsp:include page="/header.jsp"></jsp:include>

	<div class="container"
		style="width: 100%; background: url('image/regist_bg.jpg');">
		<div class="row">
			<div class="col-md-2"></div>
			<div class="col-md-8"
				style="background: #fff; padding: 40px 80px; margin: 30px; border: 7px solid #ccc;">
				<font>会员注册</font>USER REGISTER
				<form class="form-horizontal" style="margin-top: 5px;" action="/RegisterDemo/register" method="post">
					<div class="form-group">
						<label for="username" class="col-sm-2 control-label">用户名</label>
						<div class="col-sm-6">
							<input type="text" class="form-control" id="username" name="username"
								placeholder="请输入用户名">
						</div>
					</div>
					<div class="form-group">
						<label for="inputPassword3" class="col-sm-2 control-label">密码</label>
						<div class="col-sm-6">
							<input type="password" class="form-control" id="inputPassword3"  name="password"
								placeholder="请输入密码">
						</div>
					</div>
					<div class="form-group">
						<label for="confirmpwd" class="col-sm-2 control-label">确认密码</label>
						<div class="col-sm-6">
							<input type="password" class="form-control" id="confirmpwd"
								placeholder="请输入确认密码">
						</div>
					</div>
					<div class="form-group">
						<label for="inputEmail3" class="col-sm-2 control-label">Email</label>
						<div class="col-sm-6">
							<input type="email" class="form-control" id="inputEmail3"  name="email"
								placeholder="Email">
						</div>
					</div>
					<div class="form-group">
						<label for="usercaption" class="col-sm-2 control-label">姓名</label>
						<div class="col-sm-6">
							<input type="text" class="form-control" id="usercaption" name="name"
								placeholder="请输入姓名">
						</div>
					</div>
					<div class="form-group opt">
						<label for="inlineRadio1" class="col-sm-2 control-label">性别</label>
						<div class="col-sm-6">
							<label class="radio-inline"> <input type="radio"
								name="sex" id="inlineRadio1" value="male">
								男
							</label> <label class="radio-inline"> <input type="radio"
								name="sex" id="inlineRadio2" value="female">
								女
							</label>
						</div>
					</div>
					<div class="form-group">
						<label for="date" class="col-sm-2 control-label">出生日期</label>
						<div class="col-sm-6">
							<input type="date" class="form-control"  name="birthday">
						</div>
					</div>

					<div class="form-group">
						<label for="date" class="col-sm-2 control-label">验证码</label>
						<div class="col-sm-3">
							<input type="text" class="form-control">

						</div>
						<div class="col-sm-2">
							<img src="./image/captcha.jhtml" />
						</div>

					</div>

					<div class="form-group">
						<div class="col-sm-offset-2 col-sm-10">
							<input type="submit" width="100" value="注册" name="submit"
								style="background: url('./images/register.gif') no-repeat scroll 0 0 rgba(0, 0, 0, 0); height: 35px; width: 100px; color: white;">
						</div>
					</div>
				</form>
			</div>

			<div class="col-md-2"></div>

		</div>
	</div>

	<!-- 引入footer.jsp -->
	<jsp:include page="/footer.jsp"></jsp:include>

</body>
</html>




第三步:编写c3p0-config配置文件

第四步:创建数据库连接工具DataSourceUtils

package com.sh.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();
		}
	}

}

第五步:创建实体类User(字段,set,get方法,tostring)

第六步:创建servlet,进行数据的操作

package com.sh.register;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.Map;
import java.util.UUID;

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

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.QueryRunner;

import com.sh.utils.DataSourceUtils;

public class RegisterServlet extends HttpServlet {

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		// 使用BeanUtils进行自动封装映射
		// BeanUtils工作原理:将map中的数据,根据key与实体的属性的对应关系封装,只要key的名字与实体的属性的名字一样,就自动封装到实体中
		User user = new User();// User实体
		// 使用getParameterMap获取请求参数,包含表单里面所有input标签的数据,以其name为key,以其value为值
		Map<String, String[]> map = request.getParameterMap();
		try {
			BeanUtils.populate(user, map);

		} catch (IllegalAccessException | InvocationTargetException e) {
			e.printStackTrace();
		}
		// 表单中没有uid的输入,但数据库中有
		// 手动封装uid---uuid---随机不重复的字符串32位,但java代码生成后是36位
		user.setUid(UUID.randomUUID().toString());
		
		//调用register方法
		try {
			register(user);
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
	}

	// 注册的方法
	public void register(User user) throws SQLException {
		// 操作数据库
		QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());
		String sql = "insert into user values(?,?,?,?,?,?,?,?,?,?)";
		int i = qr.update(sql, user.getUid(), user.getUsername(), user.getPassword(), user.getName(), user.getEmail(), null,
				user.getBirthday(), user.getSex(), null, null);
		if (i>0) {
		System.out.println("注册成功");	
		}
	}

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

		doGet(request, response);
	}

}

8.用户登录错误回显

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值