request.getParameter返回null值:JSP,servlet

时间:2014-10-14 04:59:33

标签: jsp

我已将我的login.jsp和registration.jsp连接到我的数据库,注册成功但是当我登录时收到错误消息"密码无效"我认为我的代码有问题,请有人帮我解决,代码见下文。提前谢谢。

login.jsp

<%@ page import ="java.sql.*" %>
<%
    String userid = request.getParameter("uname");
    String email = request.getParameter("emailsignup");
    String password = request.getParameter("password");
    Class.forName("com.mysql.jdbc.Driver");
    Connection con = 
DriverManager.getConnection("jdbc:mysql://localhost:3306/mldn","root","shaddy");
    Statement st = con.createStatement();
    ResultSet rs;
    rs = st.executeQuery("select * from members WHERE uname='" + userid + "' and password='" + password +"'");
    if (rs.next()) {
        session.setAttribute("userid", userid);
        out.println("welcome " + userid);
        out.println("<a href='logout.jsp'>Log out</a>");
        response.sendRedirect("<a href='index.jsp'>Welcome</a>");
     } else {
        out.println("Invalid password <a href='index.jsp'>try again</a>");
    }
%> 

HTML

<header>
                <h1>Geo Business/Land inventory<br><span>Login And Registration</span></h1>
            </header>
            <section>               
                <div id="container_demo" >
                    <a class="hiddenanchor" id="toregister"></a>
                    <a class="hiddenanchor" id="tologin"></a>
                    <div id="wrapper">
                        <div id="login" class="animate form">
                            <form  action = 'jsp/login.jsp'> 
                                <h1>Log in</h1> 
                                <p> 
                                    <label for="username" class="uname" data-icon="u" > Your Username </label>
                                    <input id="username" name="username"  required="required" type="text" placeholder="myusername." value=""/>
                                </p>
                                <p> 
                                    <label for="password" class="youpasswd" data-icon="p"> Your password </label>
                                    <input id="password" name="password"  required="required" type="password" placeholder=" X8df!90EO" value=""/> 
                                </p>
                                <p class="keeplogin"> 
                                    <input type="checkbox" name="loginkeeping" id="loginkeeping" value="loginkeeping" /> 
                                    <label for="loginkeeping">Keep me logged in</label>
                                </p>
                                <p class="login button"> 
                                    <input type="submit" value="Login" /> 
                                </p>
                                <p class="change_link">
                                    Not a member yet ?
                                    <a href="#toregister" class="to_register">Join us</a>
                                </p>
                            </form>
                        </div>

                        <div id="register" class="animate form">
                            <form  action = 'jsp/registration.jsp'> 
                                <h1> Sign up </h1> 
                                <p> 
                                    <label for="fname" class="fname" data-icon="fn">First Name</label>
                                    <input id="fname" name="fname" required="required" type="text" placeholder="myfirstname" value=""/>
                                </p>
                                <p> 
                                    <label for="lname" class="lname" data-icon="ln" >Last Name</label>
                                    <input id="lname" name="lname" required="required" type="text" placeholder="lastname" value=""/> 
                                </p>
                                <p> 
                                    <label for="email" class="emailsignup" data-icon="e" >Email</label>
                                    <input id="email" name="email" required="required" type="email" placeholder="cug@wuhan.cn" value=""/> 
                                </p
                                <p> 
                                    <label for="username" class="uname" data-icon="u">Username </label>
                                    <input id="username" name="username" required="required" type="username" placeholder="eg. X8df!90EO" value=""/>
                                </p>
                                <p> 
                                    <label for="password" class="youpasswd" data-icon="p">Password </label>
                                    <input id="password" name="password" required="required" type="password" placeholder=" eg.X8df!90EO" value="" />
                                </p>
                                <p class="signin button"> 
                                    <input type="submit" value="Sign up"/>
                                </p>
                                <p class="change_link">  
                                    Already a member ?
                                    <a href="#tologin" class="to_register"> Go and log in </a>
                                </p>
                            </form>
                        </div>

                    </div>
                </div>  
            </section>
        </div>
    </body>
</html>

2 个答案:

答案 0 :(得分:1)

您需要确保从html表单中请求的用户名和密码正确无误。

request.getParameter方法返回输入表单字段的值,该字段在括号内给出相同的名称。

例如:

HTML:

<form action="/action.jsp">
<input type="text" value="text" name="input1">
<input type="submit" value="submit">
</form>

action.jsp:

String text=request.getParameter("input1");//will return the value 'text'

请尝试在jsp中打印值并确保值保持不变

答案 1 :(得分:0)

我犯了同样的错误,在HTML部分中,输入类型标记应该具有name属性,getParameter方法可以使用该属性来从用户那里获取输入。