[Peepagg-cvs] fake/WEB-INF/src/fake LoginServlet.java,NONE,1.1
Brought to you by:
chalko,
marccanter
|
From: <ch...@us...> - 2003-12-14 08:47:52
|
Update of /cvsroot/peepagg//fake/WEB-INF/src/fake
In directory sc8-pr-cvs1:/tmp/cvs-serv17268/WEB-INF/src/fake
Added Files:
LoginServlet.java
Log Message:
This is for testing LampIDP
--- NEW FILE: LoginServlet.java ---
/*
* Created on Nov 23, 2003
*
* To change the template for this generated file go to Window - Preferences -
* Java - Code Generation - Code and Comments
*/
package fake;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Random;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
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 javax.sql.DataSource;
/**
* @author nick
*
* To change the template for this generated type comment go to Window -
* Preferences - Java - Code Generation - Code and Comments
*/
public class LoginServlet extends HttpServlet {
/**
*
*/
public LoginServlet() {
super();
// TODO Auto-generated constructor stub
}
/*
* (non-Javadoc)
*
* @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
* javax.servlet.http.HttpServletResponse)
*/
protected void doGet(HttpServletRequest arg0, HttpServletResponse arg1)
throws ServletException, IOException {
// TODO Auto-generated method stub
super.doGet(arg0, arg1);
}
/*
* (non-Javadoc)
*
* @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest,
* javax.servlet.http.HttpServletResponse)
*/
protected void doPost(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
String userName = request.getParameter("userID");
if (userName == null || userName.trim().length() == 0) {
response.setStatus(400, "User name must be set");
return;
}
Connection conn = null;
try {
conn = getConnection();
Statement statement = conn.createStatement();
Random r = new Random();
String sessionID = Integer.toHexString(r.nextInt());
boolean success =
statement.execute(
"insert into fsn_session (userID, sessionID) values ('"
+ userName
+ "','"
+ sessionID
+ "')");
session.setAttribute("fake_userId", userName);
response.sendRedirect(
"http://localhost:8080/IDP/lamp/idp?userID="
+ userName
+ "&sessionId="
+ sessionID);
} catch (Exception e) {
response.setStatus(500, e.getMessage());
e.printStackTrace();
return;
} finally {
if (conn != null) {
try {
conn.close();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
}
}
private Connection getConnection() throws NamingException, SQLException {
Context initCtx = new InitialContext();
Context environmentContext = (Context) initCtx.lookup("java:comp/env");
DataSource ds = (DataSource) environmentContext.lookup("jdbc/fake");
Connection conn = ds.getConnection();
return conn;
}
}
|