javaweb用户密码的封装

本文介绍了JavaWeb中用户密码的封装方法,重点讨论了表单数据的获取,包括不同类型的表单输入域(如radio和checkbox)在提交时的规则。还涉及到请求参数的编码问题,特别是GET和POST请求的编码差异。文章建议在开发中采用POST提交方式,并通过一个实例展示了如何利用HttpServletRequest对象的方法封装表单数据,强调了表单域名称与JavaBean属性名一致的重要性,以便于代码的维护和修改。
摘要由CSDN通过智能技术生成
问题?在我们用表单的时候时常会遇到封装一些用户密码之类的数据这一个问题。
           这里就交给大家几个便捷方法。有常用的封装方法,也有利用开源框架BeanUtilse框架来封装方法的。

说明:

常用表单数据的获取
1.表单输入域类型:
radio checkbox,即使表单中有对应名称输入域,如果一个不选择,则什么数据不会带给服务器。(注 意空指针异常)
如果选择了其中的一个或多个,则把他们的value的取值提交给服务器。
如果选择了其中的一个或多个,他们又没有value取值,则提交给服务器的值是on.


2.请求参数的编码:
浏览器当前使用什么编码,就以什么编码提交请求参数。<meta http-equiv="content-type" content="text/html; charset=UTF-8">

request.setCharacterEncoding(编码):通知程序,客户端提交的数据使用的编码。但是只对POST请 求方式有效

3.如果是get请求提交数据,编码就是ISO-8859-1

4.Tips:目前采用POST提交方式。


举个例子:

再举封装方法之前,我们先看看HttpServletRequest中的request对象有哪些方法

package com.dp.java.Rquest;

import java.io.IOException;
import java.io.PrintWriter;

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

/**
 * HttpRequest的常用方法的使用
 *
 */
public class RequestDom1 extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		PrintWriter pr=response.getWriter();
		String url=request.getRequestURL().toString();
		//协议:主机和端口 http://localhost:8080/day7/servlet/RequestDom1
		
		String uri=request.getRequestURI();
		//得到请求资源地址/day7/servlet/RequestDom1
		
		String queryString =request.getQueryString();//get请求的参数字符串:如;表单中的username、password
		String remoteAddr =request.getRemoteAddr();
		int remotePort= request.getRemotePort();//得到客户端的端口号,注:不是服务器端口号8080
		String method =request.getMethod();//请求方式走的是什么方法:如get或者post
		
		pr.println("URL:"+url);
		pr.println("uri:"+uri);
		pr.println("queryString:"+queryString);
		pr.println("remoteAddr:"+remoteAddr);
		pr.println("remotePort:"+remotePort);
		pr.println("method:"+method);
	}

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

		doGet(request, response);
	}

}
首先是jsp或者html文件。


 
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
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 'index.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 action="/day7/servlet/RequestDom2" method="post">     <br>
  		用  户 名:<input type="text" name="username">     <br>
  		密      码:<input type="password" name="password">     <br>
  		确认密码:<input type="password" name="password">     <br>
  		性      别:<input type="text" name="gen">     <br>
  		<input type="submit"  value="提交">
  </form>

  </body>
</html>



然后是映射文件xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值