About JSP #1

Today we’ll talk about JSP, which is nothing more than language like PHP.

With JSP you can put server code into your web page, you can build a web page and everything else you can do with PHP.

Now we’ll see some directives that help us to code in JSP.

The include directive

As the name says, you use this directive to include some file to your current page. It’s very useful if you have replicated code (another jsp page) which must be in lots of pages.

Example:

<%@ include file="../include.jsp" %>

The page directive

The page directive has many utilities, so we’ll cover the most important ways of using, like using the import way. If you want to use in jsp some class you implemented in your project, you have to use import in you jsp file (it’s like the import in Java).

Example:

You want to use the class Person in your jsp. So you do:

<%@page import="com.project.Person"%>
<!-- Now you can use your class Person -->
<%
    Person person = new Person();
    person.setName(“Jacob”);
%>

The page directive can also defines some properties of your page, like the encoding type and content type. Like the default that comes in Eclipse:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

It’s very easy to use, so don’t be afraid to use that.

For more information, see this link .

Next topic we’ll see how to merge HTML and JSP, showing values computed in JSP directly in HTML. See yah!

Advertisements