Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa JSP-Servlet redirecting a login authenticated form to a form to be displayed on the same page from where the login authentication has taken place.
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Nishit Ranjan
redirecting a login authenticated form to a form to be displayed on the same page from where the login authentication has taken place.
3 Answer(s)      2 years and 4 months ago
Posted in : JSP-Servlet


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.


Advertisement
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




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 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
(); 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 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
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
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
Login Form
Login Form       Login form in struts: Whenever you goes to access data from... query for checking password  String query="select id from login where
Login form
of login form will really help to understand jsp page. In this  example we... Login Form with jsp      ... are going to insert the data in the login form which will be displayed onto
login form - JSP-Servlet
login form  Q no.1:- Creat a login form in servlets?  Hi...*; import javax.servlet.http.*; public class Login extends HttpServlet...(); pw.println(""); pw.println("Login"); pw.println(""); pw.println
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 Java - Java Beginners
= st.executeQuery("SELECT * FROM login where username='"+value1+"' && password='"+value2...Login Form in Java  Hi, In my Project, I am using Java-JDBC-Mysql... Authenticated person can access these module. I create table in my-sql where fields
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 form validation - JSP-Servlet
login form validation  hi, how to validate the login form that contains user name and password using post method . the validation should not allow user to login in the address bar thanks regards, anand
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 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
SWT login form - Swing AWT
SWT login form  Hi, I want code to create a login form in SWT. My requirement is that when the user clicks "enter" button from keyboard "login...(2, false)); shell.setText("Login form"); label1=new Label(shell
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
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
Login Form using Ajax
in the login page then a message is displayed as shown below: If you...;Password" field empty in the login page then a message is displayed as shown below...Login Form using Ajax      
login authentication - Java Beginners
login authentication  i 've designed the jsp page for login. now i need to connect it with the bean and do authentication for the login for the "register" table in mysql. can anybody help me with the code immediately
Struts Login Authentication
Struts Login Authentication  Hi Sir, Am doing a project in a struts 1.2,i want login authentication code fro that, only authenticated user can login into application,i am using back end as Mysql.so send me code as soon
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
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
Advertisements
 

 

 

DMCA.com