TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Venkatesh K H
NA
116
31.7k
How to make multiple user in one user login?
May 10 2017 6:45 AM
i have only one login page. From same login page the admin and emp should login but different landing pages appear for respective admin and emp; edit code please. there is a type in the db if emp and admin. if emp credential matches the landing page should be different else admin credential matches the landing page should be different.
package ex.com;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import javax.servlet.http.HttpSession;
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 102831973239L;
/**
* @see HttpServlet#HttpServlet()
*/
public LoginServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String email= request.getParameter("useremail");
String password = request.getParameter("password");
String searchQuery = "select * from login where email='" + email
+ "' AND password='" + password + "'";
try {
Class.forName("com.mysql.jdbc.Driver");
}
catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
}
try {
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "12345"
+ ""
+ ""
+ "");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(searchQuery);
boolean isEmpty = rs.next();
if (!isEmpty) {
// redirect to error page
request.getSession().removeAttribute("errorMessage");
request.getSession().setAttribute("errorMessage", "Invalid Credentials");
response.sendRedirect("admin.jsp");
}
else if (isEmpty) {
// fetch the session from request, create new session if session
// is not present in the request
HttpSession session = request.getSession(true);
session.setAttribute("FirstName", rs.getString("first_name"));
session.setAttribute("LastName", rs.getString("last_name"));
// redirect to success page
if("emp".equals(type))
{
response.sendRedirect("admin.jsp");
}
else if("hr".equals(type))
{
response.sendRedirect("emp.jsp");
}
}
}
}
catch (SQLException e) {
System.out.println("SQLException occured: " + e.getMessage());
e.printStackTrace();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
Reply
Answers (
1
)
How to show data which is stored in the DB ?
How to find the shortest path of nodes in java....?