Newsletter |
Java Servlet login Example In Eclipse
Servlets » on Jan 19, 2013 { 17 Comments } By Sivateja
Let us discuss one simple login application using servlet and jsp, friends please refer previous articles if you still have any doubts regarding strvlets flow 🙂
Directory Structure
Files Required
- OnServletLogin.java
- index.html
- web.xml
index.html
<form action="login" method="post"> <table> <tr> <td><font face="verdana" size="2px">Name:</font></td> <td><input type="text" name="userName"></td> </tr> <tr> <td><font face="verdana" size="2px">Password:</font></td> <td><input type="password" name="userPassword"></td> </tr> </table> <input type="submit" value="Login"> </form>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>loginServlet</servlet-name> <servlet-class>java4s.OnServletLogin</servlet-class> </servlet> <servlet-mapping> <servlet-name>loginServlet</servlet-name> <url-pattern>/login</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app>
OnServletLogin.java
package java4s; 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; public class OnServletLogin extends HttpServlet { protected void doPost(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException { PrintWriter pw=res.getWriter(); res.setContentType("text/html"); String user=req.getParameter("userName"); String pass=req.getParameter("userPassword"); if(user.equals("java4s")&&pass.equals("java4s")) pw.println("Login Success...!"); else pw.println("Login Failed...!"); pw.close(); } }
Output
​ ​​
You Might Also Like
::. About the Author .:: | ||
Comments
17 Responses to “Java Servlet login Example In Eclipse”
Can you please talk about the tag welcome-file-list. How will the parameters defined under that tag be used. How will the webserver understand that the submit button on the index.html corresponds to the servlet-mapping LoginServlet with url pattern /login???
It is very helpful
what are the benifits of servletconfig
It is used to know the configuration details of servlets like servlet name parameter value
.html file and .xml file url should same????
Login correct username and password but it’s redirect login failed
Can You explain the flow of this Program, how it works.
Thanks you very much for explaning this concept,here one more thing i didn’t understand.i,e DAO.here u didn’t used…
will u pls explain this once.
it is very helpful but I needed to check entered username and password is equal to param values entered in xml
so If you update it with that it’ll be more helpful
thanks alot
It is easy to understand..
Could you pls explain the code flow.. Am getting HTTP 405 errorHTTP method GET is not supported by this URL. Thanks!!
Could you please tell me what is the difference between servlet and webservice? and which is better?
servlet directly takes request from the end-user(browser) but webservice doesn't takes the request directly from the end-user & it takes the request from another servlet (or) any service.webservice is very important becoz it is interoperable(platform independent,language independent) & distributed……………
nice and very helpful
Is there any jar files I need to add for executing this code.
hello,it is occurring error offer login
what is error?
HTTP Status 500 – Error instantiating servlet class java4s.ServletLogin
type Exception report
message Error instantiating servlet class java4s.ServletLogin
description The server encountered an internal error that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: Error instantiating servlet class java4s.ServletLogin
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1132)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)
org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2521)
org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2510)
java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Unknown Source)
root cause
java.lang.ClassNotFoundException: java4s.ServletLogin
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1333)
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1167)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1132)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)
org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2521)
org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2510)
java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Unknown Source)
note The full stack trace of the root cause is available in the Apache Tomcat/8.0.44 logs.
Tell me how to solve HTTP Status 500 ? Internal Server Error