29.01.2013 Views

WebSphere Application Server - IBM Redbooks

WebSphere Application Server - IBM Redbooks

WebSphere Application Server - IBM Redbooks

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Get data from HTML form<br />

String user = request.getParameter("username").trim();<br />

String pass = request.getParameter("password").trim();<br />

// Verify that user exists<br />

if (hasPriv(user, pass)) {<br />

// Send user to admin page<br />

response.sendRedirect(response.encodeRedirectURL("/OnlineCatalog/administration<br />

/admin.html"));<br />

}<br />

else {<br />

PrintWriter pw = response.getWriter();<br />

pw.println("LOGIN ERROR - Invalid user or<br />

password");<br />

pw.println("Click here to Try<br />

Again");<br />

}<br />

}<br />

private boolean hasPriv(String user, String pass) {<br />

boolean result = false;<br />

try {<br />

// Connect to the database<br />

DBConnection dbConnection = new DBConnection();<br />

// Query database<br />

ResultSet rs = dbConnection.getFromDB("SELECT password, username FROM<br />

users WHERE username = '" + user + "'");<br />

if (!rs.next()) {<br />

try {<br />

common.LogWriter.getInstance().writeToLog("Login Error: Invalid<br />

username " + user);<br />

} catch (InterruptedException ex) {}<br />

return false;<br />

}//if<br />

// Get password for user from database<br />

String realPass = rs.getString("password").trim();<br />

238 <strong>IBM</strong> <strong>WebSphere</strong> <strong>Application</strong> <strong>Server</strong> - Express V5.0.2 Developer Handbook

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!