Web International Awards

payday loan

18

AUG 2010 1

Building a live news blogging system in JSP

Tutorial app preview

About 5 months ago I started a series of tutorials which described the process of building a live news blogging system in php. When the series ended, I promised that another series will follow and will describe the process of building the same web app using JSP. This article is the first of its series, and contains the introductory part of what's needed in this tutorial series, as well as some basic JSP code.

First of all, please read the first part of the original series here as it describes and shows the source codes for the user interface of the web application, namely HTML5, CSS and some jQuery.

You should also check out the second part of the original tutorial here, as this part contains the database model and some more jQuery. There's also some bits of PHP, but those aren't mandatory for today's article.

login.jsp page

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<%@ page import="java.sql.*" import="java.util.*" session="true" import="Javax.servlet.http.*" import="java.security.*" %>
 
<%
 
	String database = "lab8";
	String user = "user here";
	String pass = "pass here";
	String table = "Users";
 
	String connectionURL = "jdbc:mysql://localhost:3306/"+database+"?user="+user+";password="+pass;
	Connection connection = null;
	Statement statement = null;
	Statement update = null;
	ResultSet rs = null;
 
	Class.forName("com.mysql.jdbc.Driver").newInstance();
	connection = DriverManager.getConnection(connectionURL,user,pass);
	statement = connection.createStatement();
 
	String username = "";
	String password = "";
	int error = 0;
	if(request.getParameter("username")!=null) username = request.getParameter("username");
	else error = 1;
	if(request.getParameter("password")!=null) password = request.getParameter("password");
	else error = 1;
	if(error==1)
	{
		response.sendRedirect("index.jsp?error=Please input username and password");
	}
	else
	{
			MessageDigest mdAlgorithm = MessageDigest.getInstance("MD5");
			mdAlgorithm.update(password.getBytes());
 
			byte[] digest = mdAlgorithm.digest();
			StringBuffer hexString = new StringBuffer();
 
			for (int i = 0; i < digest.length; i++) 
			{
			    password = Integer.toHexString(0xFF & digest[i]);
 
			    if (password.length() < 2) 
				{
			        password = "0" + password;
			    }
 
			    hexString.append(password);
			}
 
			password = hexString.toString();
			String query = "SELECT * FROM Users WHERE username='"+username+"' AND password='"+password+"'";
			rs = statement.executeQuery(query);
			if(rs.next())
			{
				session.setAttribute("loggedin","1");
				session.setAttribute("username",username);
 
				response.sendRedirect("index.jsp?msg=Logged in succesfully.");
			}
			else 
			{
				response.sendRedirect("index.jsp?error=Invalid login data.");
			}
	}
%>

The first line of the code above contains instructions to include different libraries that will be used throughout the entire jsp file. The following four lines of code are in fact the variables that will be used to store database connection settings. In the next set of lines we create the datatypes that we will use to create an sql connection, perform sql queries and handle the results returned by the queries. On lines 16 to 18 we instantiate the mysql driver and connect to the database.

The instructions between lines 20 and 30 test if the visitor has inputed his username and password information in the form that send him to the login page. If the user's hasn't filled in required information, the request is denied. Lines 33 to 50 are used to encrypt the password the user typed in the login form. Passwords in the database are encrypted to ensure that, even if third parties gain access to the database, the passwords aren't exposed.

The following two lines make an SQL query to check if the user credentials are valid. If they are, lines 56 to 59 are used to create required login sessions and redirect the user to the homepage, while the code on line 63 redirects the user to the homepage after an invalid login.

logout.jsp

The logout process is much more simple, and the following code snippet does the trick. This snippet also contains the imports found in the login.jsp file, one line of code that destroys the session and one that redirects the user to the homepage.

1
2
3
4
5
6
<%@ page import="java.sql.*" import="java.util.*" session="true" import="Javax.servlet.http.*" import="java.security.*" %>
 
<%
	session.removeAttribute("loggedin");
	response.sendRedirect("index.jsp?msg=Successfully logged out");
%>

While the php application also contained a functions.php file, this does not. Having that said, this is the end of the first part of the tutorial. You can stay updated to new content via our RSS feed or by email.

Published on Wednesday, August 18th, 2010 at 6:24 am in tutorials.

About Bogdan Pop

Bogdan Pop is a young Romanian entrepreneur who runs WebRaptor. He is a web developer with awesome design skills, who enjoys writing about everyday's work and usability. He relaxes by taking photos every once in a while and by mixing french electronic music. Connect with him via Twitter.
 
  1. Building a live news blogging system in JSP [II] says: August 25th, 2010 at 12:01 pm

    [...] week I started building a live news blogging system using JSP. This is a follow up tutorial I've decided to do after seeing the success of the previous series, [...]





Save time next time! You won't have to fill out all these fields again. Register in just a few clicks and then login.


If you do not have a username, you can register in just a few clicks.