A Login web using Java Servlet and mySQL, problems about connected to MySQL -


i wrote login web , can't work well. may problem of mysql connecting.

here code. it's simple. there servletlogin.java , index.jsp.

servletlogin.java

package login;  import java.io.ioexception; import javax.servlet.servletexception; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import javax.servlet.http.httpsession;  import java.sql.*;  public class servletlogin extends httpservlet {     private static final long serialversionuid = 1l;     private string name;     private string pass;      public servletlogin() {         super();         // todo auto-generated constructor stub     }     protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {         // todo auto-generated method stub         dopost(request, response);     }     protected void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {         // todo auto-generated method stub         string printout = "";         this.name = request.getparameter("username");         this.pass = request.getparameter("password");                if (name == "" || name == null || name.length() > 20) {             try {                 printout = "20字以内ユーザネームを再入力ください";                 request.setattribute("message", printout);                 response.sendredirect("index.jsp");                 return;             } catch (exception e) {                 // todo: handle exception                 e.printstacktrace();             }         }         if (pass == "" || pass == null || pass.length() > 20) {             try {                 printout = "20字以内のパスワードを再入力ください";                 request.setattribute("message", printout);                 response.sendredirect("index.jsp");                 return;             } catch (exception e) {                 // todo: handle exception                 e.printstacktrace();             }         }         // database driver          try {             class.forname("com.mysql.jdbc.driver");         } catch (exception e) {             // todo: handle exception             system.out.println("class not found exception");         }         // create url            string url = "jdbc:mysql://localhost:3306/databasedemo";          try {             connection conn = drivermanager.getconnection(url,"root","");             statement stmt = conn.createstatement();             string sql = "select * userinfo username='"+name+"' , password= '"+pass+"'";             resultset rs = stmt.executequery(sql);              if (rs.next()) {                 if (this.name.equals(rs.getstring(1))||this.pass.equals(rs.getstring(2))) {                     response.sendredirect("success.jsp");                 }             }         } catch (exception e) {             // todo: handle exception             e.printstacktrace();         }        } } 

index.jsp

<%@ page language="java" contenttype="text/html; charset=utf-8"     pageencoding="utf-8"%> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <title>ログイン</title> <script type="text/javascript">  function changeimg()  {     document.getelementbyid("validatecodeimg").src="${pagecontext.request.contextpath}/checkcodeimage?" + math.random(); } </script> </head> <body> <form action="${pagecontext.request.contextpath}/servletlogin">     <table border="0" align="center">         <tr height="30"></tr>         <tr align="center">             <td colspan="2"><font size="20">java web</font></td>         </tr>         <tr height="30"></tr>         <tr height="50">             <td>ユーザネーム</td>             <td><input type="text" name="username" placeholder="ユーザネームを入力してください"></td>         </tr>         <tr height="50">             <td>パスワード</td>             <td><input type="password" name="password" placeholder="パスワードを入力してください"></td>         </tr>          <tr height="50">             <td><img alt="認めない" src="${pagecontext.request.contextpath}/checkcodeimage"              id="validatecodeimg" onclick="changeimg()">             </td>             <td><input type="text" name="checkcode" placeholder="右側の文字を入力してください"></td>         </tr>         <tr height="50">             <td colspan="2" align="center">                 <input type="submit" name="login" value="ログイン">             </td>         </tr>     </table> </form>  </body> </html> 

and here database called databasedemo.

+--------+----------+----------+
| userid | username | password |
+--------+----------+----------+
| 1 | jack | 123456 |
| 2 | mary | 654321 |
+--------+----------+----------+

i think stucked command in servletlogin.java. supposed submit form , turn servletlogin.java , command.

if username , password null, recording code in servlet.java, go index.jsp. , went well.

but if username , password same in database, should have go page success.jsp. when run it, shows nothing. seems has not been submitted , command in servletlogin.java. , url shows http://localhost:8080/logindemo/servletlogin?username=jack&password=123456&checkcode=axww&login=%e3%83%ad%e3%82%b0%e3%82%a4%e3%83%b3

i'm confused of it. think have problem connecting mysql. don't know how fix it. please me it...


update

i have solved problem!

when run code, exception shows class not found exception java.sql.sqlexception: no suitable driver found jdbc:mysql://localhost:3306/databasedemo. exception of jdbc driver.
when exception shows, there's 4 reason:

  1. wrong url. please check right code: connection conn=drivermanager.getconnection("jdbc:mysql://localhost:3306/xx","root","xxxx"
  2. wrong driver declaration, right 1 :com.mysql.jdbc.driver
  3. mysql-connector-java-5.1.40.jar must same version mysql.
  4. mysql-connector-java-5.1.40.jar has been put wrong position. right position copy in web-inf file.

as me, put .jar in wrong position. , when copy in file web-inf, code run well. hope someone. , code above simple webpage of login. continue complete java project.

and answered question. @ravi kumar pointed out 1 of mistakes, , corrected it. it's big mistake of me using mysql.

my best guess query

select * userinfo username='"+name+"' , password= '"+pass+"'" 

this select 1 | jack | 123456

now rs.getstring(1)=1 ie id , rs.getstring(2) jack ie. username

instead use

rs.getstring("username"); rs.getstring("password"); 

you may change query

select username,password userinfo username='"+name+"' , password= '"+pass+"'" 

in order make work existing code


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -