Kuukausi: tammikuu 2014

Simple Java Servlet Example

I am currently attending at Java EE course and I made a simple example how to send information to another page. This was part of course homework. This program takes user book information from index.jsp and sends it to another jsp-page.

First we have to create Book Bean:

package com.iinurmi.wordpress.beans;

public class Book {
	
	private String name;
	private String isbn;
	
	public Book(String name, String isbn) {
		this.name = name;
		this.isbn = isbn;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getIsbn() {
		return isbn;
	}
	public void setIsbn(String isbn) {
		this.isbn = isbn;
	}
	
	@Override
	public String toString() {
		return "Book [name=" + name + ", isbn=" + isbn + "]";
	}
}

Just a simple bean which has the default constructors and methods.

Next step is to create the jsp page and the servlet which will direct the information for another page.

Index jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>Books</title>
	</head>
	<body>
		<p>Tervetuloa Iiro Nurmen täydelliseen kirjajärjestelmään!</p>
		<form action="sendbook" method="post">
			<table>
				<tr><td>Kirjan Nimi</td><td><input type="text" name="bookname"/></td</tr>
				<tr><td>ISBN</td><td><input type="text" name="isbn"/></td></tr>
				<tr><td>&nbsp;</td><td><button type="submit">Lähetä!</button></td></tr>
			</table>
		</form>
	</body>
</html>

The form action tells where the request will be sent. This is where you put your servlet name. I’m using post as a method. Next we have to create the servlet, which takes the info and sends it to next page.

package com.iinurmi.wordpress.servlets;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.iinurmi.wordpress.beans.*;

/**
 * Servlet implementation class showBook
 */
@WebServlet("/sendbook")
public class SendBook extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public SendBook() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
			
			String bookn = request.getParameter("bookname");
			String is = request.getParameter("isbn");
			
			Book book = new Book(bookn,is);
			
			request.setAttribute("book1", book);
			request.getRequestDispatcher("books.jsp").forward(request, response);
			
	}

}

That is a basic servlet that takes request and response and forwards it to jsp-called books.jsp. First this servlet finds parameter ”bookname” and ”isbn” from request. These are values from index.jsp. These values are storet to local variables. From local variables we create a book object. Book object is the bean which we created in the beginning. The book object is then saved as a request attribute ”book1”. Then we forvard request and response to the page where the information will be shown.

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Books</title>
</head>
<body>
	<table>
		<tr><th>Kirjan nimi: </th><td><c:out value="${book1.name}"/></td></tr>
		<tr><th>Kirjan ISBN: </th><td><c:out value="${book1.isbn}"/></td></tr>
	</table>
	<a href="index.jsp">Takaisin kirjan syöttövaiheeseen</a>
</body>
</html>

Here we simply print out book objects information. This is done by jstl (Java Standard Tag Library). With jstl, it is much more easier and faster to print to jsp pages.

Quick Update: theme

Yep! I changed the wordpress theme and the code parts are all over the place. WordPress has not been the best option if you want to paste code here. I will probably be posting more code as a file or in smaller parts that it will be readable.

I still think that the new appearance is much more cooler than the old one.

Project Italian Pizza Restaurant

Last semester me and my team made a pizzeria web service for fictional pizzeria Lorenzo. We used agile development method, scrum, coded with java and used tomcat for server. In the website you can order pizza and drinks online. The information goes do database and the delivery is made.

Pizzeria Lorenzo frontpage

Pizzeria Lorenzo frontpage

When user arrives to the ordering page, the information will be downloaded from the pizzerias database and compiled to the menu. The drinks are listed in other tab, thanks for jquery UI.
In the ordering page you can choose which pizza you want how many etc. The order will be kept in the cart for the session. If the user accidentally presses the back button or another page, the order will be in the cart.

"Ordering" -page

”Ordering” -page

After rthe order is ready the user will have to fill in information of himself and the payment.

User information filling

Users can’t fill in the wrong information, or the order will not get trough.

Finally the the ”receipt” is printed to the user and sent to database.

Order Information -page

Order Information -page

Time Bomb Defuse Over 1000!

Time Bomb Defuse has recieved over 1000 dowloads. This came by suprise, but a pleasant suprise!
Here some statistics:

Explanations from left: name / price (free) / current users / total installs / average ratings / total ratings.

Explanations from left: name / price (free) / current users / total installs / average ratings / total ratings.

Install statistics:

Total installs

Total installs

Installs per day

Installs per day

Current users

Current users