Java Little Web Server Tutorial
Any servlet should extend LittleServlet abstract class and override the main method.
All compiled classes should be placed in the subdirectories of the server public directory according to the class' package name.
To access servlet trougth your browser, type http://localhost/<path><sevletClassName>.dthml. Mapped extension dhtml can be changed in the configuration file.
Predefined variables
The predefined objects are: request (HTTPRequest), response (HTTPResponse) and session (HTTPSession).
HelloWorld application
import ro.polak.littleServlet.*;
public class HelloWorld extends LittleServlet {
public void main()
{
PrintWriter out = response.getPrintWriter();
out.print("Hello world!");
}
}
Accessing GET and POST attributes
import ro.polak.littleServlet.*;
public class TestGetPost extends LittleServlet {
public void main()
{
PrintWriter out = response.getPrintWriter();
out.println("GET name is "+request._get("name"));
out.println("POST name is "+request._post("name"));
}
}
Using cookies
Cookies are accessed trought request.getCookie(cookieName) and response.setCookie(cookieName, cookieValue).
import ro.polak.littleServlet.*;
public class TestSession extends LittleServlet {
public void main()
{
PrintWriter out = response.getPrintWriter();
if(request.getCookie("exampleAttributeName") == null)
{
out.print("Cookie variable has not been set yet.");
}
else
{
out.print("Cookie value is "+request.getCookie("exampleAttributeName"));
}
response.setCookie("exampleAttributeName", "CookiesWork!");
}
}
Using session
import ro.polak.littleServlet.*;
public class TestSession extends LittleServlet {
public void main()
{
PrintWriter out = response.getPrintWriter();
if(session.getAttribute("exampleAttributeName") == null)
{
out.print("Session variable has not been set yet.");
}
else
{
out.print("Session variable is "+session.getAttribute("exampleAttributeName"));
}
session.setAttribute("exampleAttributeName", "SessionWorks!");
}
}
