Created
October 2, 2013 10:31
-
-
Save swapnil-webonise/6791750 to your computer and use it in GitHub Desktop.
Creating Session Using Servlet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package studentMain; | |
import java.io.IOException; | |
import javax.servlet.ServletException; | |
import javax.servlet.http.HttpServlet; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import javax.servlet.http.HttpSession; | |
import java.sql.Connection; | |
import java.sql.PreparedStatement; | |
import java.sql.ResultSet; | |
import java.io.PrintWriter; | |
import java.sql.*; | |
public class StudentLogin extends HttpServlet { | |
private static final long serialVersionUID = 1L; | |
Connection con; | |
ResultSet rs,rs1; | |
PreparedStatement ps; | |
HttpSession session ; | |
public StudentLogin() { | |
super(); | |
con=ConnectionClassForMyProjectWindow.getConnection(); | |
System.out.println("Conection succefull "); | |
} | |
public void process(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException | |
{ | |
try | |
{ | |
String user=request.getParameter("studentid"); | |
String pass=request.getParameter("pass"); | |
String SQLQuery=" select * from student_personal_details where student_Id ='"+ user +"'"+ | |
" AND password = '"+pass+"'"; | |
String SQLQuery2=" select cm.course_Name,cm.stream_Name,sbr.insitiute_sem_Id,sbr.instiute_Id "+ | |
" from student_batch_rollno sbr "+ | |
" inner join course_master cm on cm.course_Id=sbr.course_id "+ | |
" where sbr.student_Id='"+user+"'"; | |
ps = con.prepareStatement(SQLQuery); | |
rs = ps.executeQuery(); | |
rs.last(); | |
String name=rs.getString(3).concat(" "+rs.getString(4)+" ").concat(rs.getString(5)); | |
int size=rs.getRow(); | |
PrintWriter out=response.getWriter(); | |
ps=con.prepareStatement(SQLQuery2); | |
rs1=ps.executeQuery(); | |
rs1.last(); | |
String course=rs1.getString(1)+" "+rs1.getString(2); | |
String institute_sem_id=rs1.getString(3); | |
String institute_id=rs1.getString(4); | |
if(size==1) | |
{ | |
session=request.getSession(); | |
session.setAttribute("userid", user); | |
session.setAttribute("username", name); | |
session.setAttribute("coursename", course); | |
session.setAttribute("institute_sem_id", institute_sem_id); | |
session.setAttribute("institute_id", institute_id); | |
out.write("1"); | |
} | |
else | |
{ | |
out.write("0"); | |
} | |
} | |
catch(SQLException e){ | |
System.out.println(e); | |
} | |
catch(Exception e){ | |
System.out.println(e); | |
} | |
} | |
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException | |
{ | |
this.process(request, response); | |
} | |
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException | |
{ | |
this.process(request, response); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment