Monday, November 12, 2012

Basic Login Program using JSP and GenericSevlet class


To see Steps of Creating First Project Click here 
Step 1: In Projects Window double click on index.jsp file present in Web Pages Folder and Write Following Html Code.
Inside Form tag we have written action where we have passed NewServlet that is Url-Pattern of Servlet-Name NewServlet that will Redirect current page to NewServlet.java Page.
 Second thing inside form tag is method which are of two types:
a) Get(default)
b) Post

Get will also work if we don't pass any method property inside form tag and if we are using Get method then on submit button click when page Redirect to NewServlet.java you can see values of username and password on Navigation bar that is Security Risk that's why we use post method to hide entered values.
Get and Post methods are also used to call doGet() and doPost() methods respectively on Servlet page.

Step 2: Double Click on Web.Xml file in WEB-INF Folder and click on XML tab.

Here You can also Change Url-Pattern of your Servlet and Welcome-file of your project by replacing index.jsp with your file inside welcome-file tag.

Step 3: NewServlet.java code


 

Here we are using getParameter() to get username, password and Storing them in String s1 and s2 then comparing Sting s1 value with Himanshu(username) and String s value with Bhasin(password) using equals() method, if username and password both are true then output will be Welcome Himanshu(username) otherwise it'll display InValid user! 

java.​io Package
public class IOException extends Exception
Signals that an I/O exception of some sort has occurred. This class is the general class of exceptions produced by failed or interrupted I/O operations.

public class PrintWriter extends Writer
Prints formatted representations of objects to a text-output stream. This class implements all of the print methods found in PrintStream.

javax.​servlet Package
public class ServletException extends Exception
Defines a general exception a servlet can throw when it encounters difficulty.


java.​io.​PrintWriter
public void println()
Terminates the current line by writing the line separator string. The line separator string is defined by the system property line.separator, and is not necessarily a single newline character ('\n').

 Download Code Link 1
 Download Code Link 2

Output:
Run indx.jsp
 After submit button click

No comments:

Popular Posts