redirecting a login authenticated form to a form to be displayed on the same page from where the login authentication has taken place.

redirecting a login authenticated form to a form to be displayed on the same page from where the login authentication has taken place.

I am successfully able to create a session on successful login, but I want to display the welcome page on the parent page itself instead of getting redirected to welcome.jsp,whisch is a different page altogether.

Please Help Urgently.

View Answers

June 1, 2012 at 5:14 PM

The given code accepts username and password from the user and check whether the user is valid. If user is valid, welcome message will get displayed. Otherwise, the login will get redirected again.

1)login.jsp:

<html>
<script>
function validate(){
var username=document.form.user.value;
var password=document.form.pass.value;
if(username==""){
 alert("Enter Username!");
  return false;
}
if(password==""){
 alert("Enter Password!");
  return false;
}
return true;
}
</script>
<form name="form" method="post" action="http://localhost:8080/examples/Login" onsubmit="javascript:return validate();">
<table>
<tr><td>Username:</td><td><input type="text" name="user"></td></tr>
<tr><td>Password:</td><td><input type="password" name="pass"></td></tr>
<tr><td></td><td><input type="submit" value="Submit"></td></tr>
</table>
</form>
</html>

2)Login.java:

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class Login extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet { 

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try{
        response.setContentType("text/html");
        PrintWriter out=response.getWriter();
        String user=request.getParameter("user");
String pass=request.getParameter("pass");
 Class.forName("com.mysql.jdbc.Driver").newInstance();
    Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");  
           Statement st=con.createStatement();
           ResultSet rs=st.executeQuery("select * from login where username='"+user+"' and password='"+pass+"'");
int count=0;
          while(rs.next())
          {

                   count++;
          }

                    if(count>0)
          {
            out.println("welcome "+user);
          }
          else
          {
                       response.sendRedirect("/examples/jsp/login.jsp");
          }
    }
    catch (Exception e) {
    e.printStackTrace();
    }
}
}

June 1, 2012 at 5:16 PM

The given code accepts username and password from the user and check whether the user is valid. If user is valid, welcome message will get displayed. Otherwise, the login page will get redirected again with error message.

1)login.jsp:

<html>
<script>
function validate(){
var username=document.form.user.value;
var password=document.form.pass.value;
if(username==""){
 alert("Enter Username!");
  return false;
}
if(password==""){
 alert("Enter Password!");
  return false;
}
return true;
}
</script>
<form name="form" method="post" action="check.jsp" onsubmit="javascript:return validate();">
<table>
<tr><td>Username:</td><td><input type="text" name="user"></td></tr>
<tr><td>Password:</td><td><input type="password" name="pass"></td></tr>
<tr><td></td><td><input type="submit" value="Submit"></td></tr>
</table>
</form>
</html>
<%String msg=request.getParameter("msg");
if(msg!=null){
    %>
<label><font color="red"><%=msg%></font></label> 
<%
}
    %>

2)check.jsp:

<%@page import="java.sql.*"%>

<%
String user=request.getParameter("user");
String pass=request.getParameter("pass");
 Class.forName("com.mysql.jdbc.Driver").newInstance();
    Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","root");  
           Statement st=con.createStatement();
           ResultSet rs=st.executeQuery("select * from login where username='"+user+"' and password='"+pass+"'");
int count=0;
          while(rs.next())
          {

                   count++;
          }

                    if(count>0)
          {
            out.println("welcome "+user);
          }
          else
          {
                       response.sendRedirect("login.jsp?msg=Invalid Username or Password");
          }
%>

June 1, 2012 at 5:17 PM

Ads









Related Tutorials/Questions & Answers:
redirecting a login authenticated form to a form to be displayed on the same page from where the login authentication has taken place.
redirecting a login authenticated form to a form to be displayed on the same page from where the login authentication has taken place.  I am...=st.executeQuery("select * from login where username='"+user+"' and password='"+pass+"'"); int
Login Form
of +,on clicking which a login form appears. Only the middle column is different for each of the page. How to write a code for login authentication so...Login Form  I have 8 jsp pages.Each of them has three columns:Left
Advertisements
Login Form
of +,on clicking which a login form appears. Only the middle column is different for each of the page. How to write a code for login authentication so...Login Form  I have 8 jsp pages.Each of them has three columns:Left
login form
(); ResultSet rs=st.executeQuery("select * from login where username='"+value1...login form  sir my next form consists logout button when i click on it it showing login form but next form window is not closing but the components
login form
=con.createStatement(); ResultSet rs=st.executeQuery("select * from login where...login form  sir my next form consists logout button when i click on it it showing login form but next form window is not closing but the components
login and registration form using servlet and authentication through mysql
login and registration form using servlet and authentication through mysql  i made a simple login and rgistration form using servlet and authentication through mysql and whenevr i run it on tomcat a blank page is displayed plz
Login form and registration
Login form and registration  I need a complete code for ligin and new user registration form and validation
administrator login form
administrator login form   Hi iam using struts frame work backend as oracle .i want code for administrator login form thanks in advance
Spring login form application
Spring login form application  Hi Please give me the Spring login form application so that i can understand its flow ?   Hi please find the link of the tutorial Spring Applications
java login form using netbeans
java login form using netbeans  how to connect an access database to a login form using netbeans
verfication of user login and then redirecting it to user defined page
verfication of user login and then redirecting it to user defined page  hi, i have a login page that..calls the servlet..and i have used redirect...,the html page calls teh servlet..but..the servlet.is not redirecting to a user defined
Spring alternative to Form Based Login
. Using a login form we can use the jspringsecurity_check. How can we do it by sending a web service call and not using any form based authentication...Spring alternative to Form Based Login  print("code sample");how can
Login authentication
Login authentication  i want d code for login authentication from mysql database on the same pc using swings
Login authentication
Login authentication  i want d code for login authentication from mysql database on the same pc using swings
standart login form for messenger in java
standart login form for messenger in java  hi i need login from for chating messenger in java. but its structure would like this Home Register Login User ID Password   Hi Friend
standart login form for messenger in java
standart login form for messenger in java  hi i need login from for chating messenger in java. but its structure would like this Home Register Login User ID Password   Hi Friend
login for different user in the same page
login for different user in the same page  how can i do the login for different user in the same page throug jsp
Login form in jscript
Login form in jscript  For example the html script is <...; charset=utf-8" /> <title>:: validation form::</title> <script...;td align="center" valign="top"> <form name="form1" onsubmit
Struts 2.1.8 Login Form
Struts 2.1.8 Login Form      ... to validate the login form using Struts 2 validator framework. About the example: This example will display the login form to the user. If user enters Login Name
How to make login form in Struts 2?
How to make login form in Struts 2?  Hi, How to make login form in Struts 2? Thanks   Hi, If you want to learn to make Login form in Struts then check following tutorial: How to create Login Form in Struts2? Thanks
What is the use of Login Page?
this: Login Page is the page where you can get authenticated before....   Login Form is the page where you can login... to register if they are getting the login form from a login page. By default
login form using java bean in eclipse
login form using java bean in eclipse  I have made a college community website but i need to implement beans to my login and contact us page. how can i do
Redirect to same jsp page on successful login
Redirect to same jsp page on successful login  Hi, I have an application where on successful login, the redirect should happen to same page... from the database. I am unable to create the session for the same login page
For my website,As soon as the user is able to succesfully login, a Testimonial form appears.
For my website,As soon as the user is able to succesfully login, a Testimonial form appears.  For my website,As soon as the user is able to succesfully login, a Testimonial FORM appears in the same page. How to do. Please Help
How to put the image in footer in login form ?
(); ResultSet rs=st.executeQuery("select * from login where username...How to put the image in footer in login form ?  Hi, how to put the image in footer in login form ?   Here is an example that shows image
Example of login form validation in struts2.2.1framework.
Example of login form validation in struts2.2.1 framework. In this example, we will introduce you to about the login form validation in struts2.2.1 framework. Our login form validation example does not validate the user against
how to login form through spring dao module
how to login form through spring dao module  here i want to chek user details in database through form by using spring dao module.please give me some reference example to me
getting error in your login form code
getting error in your login form code  i tried your code for login form but i am getting an error.the error is undefined index userid...   hi friend, your form's input must have following : <input type
Spring 3 MVC Login Form Example
Spring 3 MVC Login Form Example  Spring 3 MVC Login Form Example index.jsp file have code.... Simple Form request will be handle...()); } My question is that, using above code how WEB-INF/views/simpleForm.jsp page
Spring 3 MVC Login Form Example
Spring 3 MVC Login Form Example  Spring 3 MVC Login Form Example index.jsp file have code.... Simple Form request will be handle...()); } My question is that, using above code how WEB-INF/views/simpleForm.jsp page
Display Logo on login form using swing
to display a logo in login form using swing components. Here is an example where... displayed on the login form. Example: import javax.swing.*; import java.sql....("select * from login where username='"+value1+"' and password='"+value2

Ads