java web中表单 与MySQL的连接 以及中文乱码的解决

<span style="font-family:SimHei;font-size:48px;">注意:所有页面编码 如pageEncoding或charset 与数据库 都保持一致 一般为utf-8,如果乱码再将连接数据库的JSP内加入</span>
<span style="font-family:SimHei;font-size:48px;">request.setCharacterEncoding("utf-8");便可解决乱码问题</span>




登陆代码


<%@ 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 'login.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>
  
  
  <h1>登录操作</h1>
  <hr/>
  <form action="login_check.jsp" type="post">
    <table border="1">
    <tr>
    <td colspan="2">用户登录</td>
    </tr>
    <tr>
    <td>登录ID:</td>
    <td><input type="text" name="id"></td>
    </tr>
    <tr>
      <td>登录密码:</td>
      <td><input type="password" name="password"></td>
    </tr>
    <tr>
       <td colspan="2">
       <input type="submit" value="登录">
       <input type="reset" value="重置">"
       </td>
    </tr>
    </table> 
    </form>
</html>

 
登录检查代码
</pre><pre name="code" class="plain"><pre name="code" class="plain"><%@ page language="java" import="java.util.*,java.sql.*" 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 'login_check.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>
<%!
//定义数据库驱动程序
public static final String DBDRIVER="com.mysql.jdbc.Driver";
public static final String DBURL="jdbc:mysql://localhost:3306/mldn";
public static final String DBUSER="root";
public static final String DBPASS="123456";
 %>
<%
Connection conn=null; //声明数据库连接对象\
PreparedStatement pstmt=null; //声明数据库操作
ResultSet rs=null; //声明数据库结果集
boolean flag=false; //定义标志位
String name=null; //接收用户的真实姓名
 %>
 <%
 request.setCharacterEncoding("utf-8"); //防止乱码
 try{
  Class.forName(DBDRIVER); //加载驱动程序
  conn=DriverManager.getConnection(DBURL,DBUSER,DBPASS); //取得数据库连接
  //编写要使用的SQL语句,验证用户id和密码,如果正确,则取出真实姓名
  String sql="SELECT name FROM user WHERE userid=? AND password=?";
 pstmt=conn.prepareStatement(sql); //实例化数据库操作对象
 pstmt.setString(1,request.getParameter("id")); //设置查询所需要的内容
 pstmt.setString(2,request.getParameter("password")); //设置查询所需要的内容
 rs=pstmt.executeQuery(); //执行查询
 if(rs.next()){  //如果可以查询得到,则表示合法用户
 name=rs.getString(1); //取出真实姓名
 flag=true;
 }
 }catch(Exception e){
 System.out.println(e);
 }finally{
   try{
   rs.close();//关闭查询对象
   pstmt.close();//关闭操作对象
   conn.close();//关闭数据库连接
 }catch(Exception e){
 }
 }
  %>
 <%
  if(flag) { //登陆成功,跳转到成功页
  %>
    <jsp:forward page="login_success.jsp">
      <jsp:param name="uanme" value="<%=name %>"/>
     </jsp:forward>
     <%
     }else{  //登录失败,跳转到失败页
      %>
      <jsp:forward page="login_failurel.html"/> <!-- 执行跳转操作 -->
      <%
      }
       %>
  </body>
</html>

登录成功界面代码

 
 
<pre name="code" class="plain"><%@ 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 'login_success.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>
<h1>登录操作</h1>
<h2>登录成功</h2>
<h2>欢迎<font color="red">
<%=request.getParameter("uanme") %>  光临!
</font></h2>
  </body>
</html>


 
 
</pre><pre name="code" class="plain">
登录失败代码
</pre><pre name="code" class="plain"><pre name="code" class="plain"><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>login_failurel.html</title>
	
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

  </head>
  
  <body>
<h1>登录操作</h1>
<h2>登录失败,请重新<a href="login.jsp">登录</a>!</h2>

  </body>
</html>


 
 
</pre><pre name="code" class="plain">
</pre><pre name="code" class="plain">

你可能感兴趣的:(java web中表单 与MySQL的连接 以及中文乱码的解决)