Home > J2EE, JSP, Technical > First JSP Servlet application

First JSP Servlet application

February 29, 2012

Note: for any changes in servlet class, you have to restart the tomcat server to see the changes 😦

LoginFormServlet.jsp

———————————–

<html>
<head>
<title>Login Form</title>
<style type=”text/css”>
div {
margin: 250px,450px;
}
</style>
</head>
<body>
<form action=”/LoginValidation” method=”post” align=center>
<div>
<table border=0>
<tr>
<td>
User Name:
</td>
<td>
<input type=”text” name=”user” />
</td>
</tr>
<tr>
<td>
Password:
</td>
<td>
<input type=”password” name=”pass” />
</td>
</tr>
<tr>
<td colspan=2 align=right>
<input type=”submit” value=”Submit” >
</td>
</tr>
</table>
</div>
</form>
</body>
</html>

LoginValidation.java (Servlet Class)  Compile and copy the class file to Classes folder

———————————————————-

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class LoginValidation extends HttpServlet
{
public void doGet(HttpServletRequest request,HttpServletResponse response)
{

try
{
String userName = request.getParameter(“user”);
String passWord = request.getParameter(“pass”);
PrintWriter out = response.getWriter();

if(userName.equals(“karu”)  && passWord.equals(“karuvachi”))
{
out.println(“Congratulations… you can work on this application :-)”);
}
else
{
out.println(“Sorry… you cannot work on this application :-(“);
}
}catch(Exception e) { System.out.println(e);}
}
public void doPost(HttpServletRequest request,HttpServletResponse response)
{
doGet(request,response);
}
}

web.xml

——————————-

<web-app>

………………………………………….

………….

<servlet>
<servlet-name>LoginValidation</servlet-name>
<servlet-class>LoginValidation</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginValidation</servlet-name>
<url-pattern>/LoginValidation</url-pattern>
</servlet-mapping>

</web-app>

Advertisements
Categories: J2EE, JSP, Technical
%d bloggers like this: