stilted 2016-06-22 02:33 采纳率: 50%
浏览 3738
已结题

request.getParameter无法获取前台的值

---------------------login.html----------------------------

 <script type="text/javascript">

$(function(){inEvent();});

function inEvent(){$("#regSubmit").click(formSubmit);

$("#divdialog").css("display","none");}


function formSubmit() {$.post("LoginServlet",$("#form2").serialize(),function (data) {if (data == 1) {
$("#divdialog").css("display", "");

$("#divdialog").html("登陆成功");
$("#divdialog").dialog({title:"登陆成功提示",modal:true,buttons:
{"确定并跳转":function(){$(this).dialog("close");
location.href="index.html";
},"关闭":
function(){$(this).dialog("close");}}});}else{alert("data值为0"); $("#divdialog").html("有错误发生,可能是用户名或密码错误!");$("#divdialog").dialog({title:"登陆失败提示",modal:true,buttons:{"关闭":function(){$(this).dialog("close");}}});}});}

</script>  
</head>

<body>
<div class="main">
<div class="login">
<h1>学生管理系统</h1>
<div class="inset">

<form  method="post" id="form2" name="form2">
     <div>
        <h2>登录</h2>
            <span><label>用户名</label></span>
            <span><input name="tbusername" type="text" id="tbusername" class="textbox" ></span>
            </div>
            <div>
        <span><label>密码</label></span>
        <span><input name="tbpassword" type="password" id="tbpassword" class="password"></span>
    </div>
  <div class="sign">
   <input name="regSubmit" type="button" value="登录" id="regSubmit" class="submit" />
  <a href='reg.html'><input type="button" value="注册" class="submit" /></a>
</div> 
<div id="divdialog" title="我是JQuery,欢迎您的到来">
<p>JQuery模态对话框:确定删除用户信息?</p>
</div>                     
</form>

-----------------------servlet------------------------

 import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

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

        response.setContentType("text/html");
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");  
        String tbusername=  request.getParameter("tbusername");     
        String tbpassword=  request.getParameter("tbpassword");     

        System.out.println(tbusername); //返回null
        System.out.println(tbusername);//返回null

        try {

            Class.forName("com.mysql.jdbc.Driver");
            Connection cn=DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/student","root","123456");
            PreparedStatement ps=cn.prepareStatement("SELECT * FROM userreg WHERE username=? AND pwd=?");
            ps.setString(1,tbusername);
            ps.setString(2,tbpassword);
            ResultSet rs=ps.executeQuery();

            if(rs.next()){
//              System.out.println("密码对");
                response.getWriter().print("1");
            }else{
//              System.out.println("密码错");
                response.getWriter().print("0");
            }
        } catch (Exception e) {

            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

}


  • 写回答

3条回答 默认 最新

  • Bug开发攻城狮 2016-06-22 02:37
    关注

    小兄弟,你的这个排版,看着辣眼啊

    你把 $("#form2").serialize(),在前台打印,看是否真有值
    看下,你前台的属性名是否和后台的paremeter名一致

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 9月2日

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?