Getting started with Winstone. Minimal servlet container

Similar documents
Introduction to Servlets. After which you will doget it

Servlet for Json or CSV (or XML) A servlet serving either Json or CSV (or XML) based on GET parameter - This version uses org.json

Backend. (Very) Simple server examples

Servlets by Example. Joe Howse 7 June 2011

Accessing a database from Java. Using JDBC

Tiers (or layers) Separation of concerns

ServletConfig Interface

Servlet. 1.1 Web. 1.2 Servlet. HTML CGI Common Gateway Interface Web CGI CGI. Java Applet JavaScript Web. Java CGI Servlet. Java. Apache Tomcat Jetty

Session 9. Introduction to Servlets. Lecture Objectives

Session 8. Introduction to Servlets. Semester Project

Web based Applications, Tomcat and Servlets - Lab 3 -

JAVA SERVLET. Server-side Programming INTRODUCTION

Servlets1. What are Servlets? Where are they? Their job. Servlet container. Only Http?

Advanced Internet Technology Lab # 4 Servlets

sessionx Desarrollo de Aplicaciones en Red A few more words about CGI CGI Servlet & JSP José Rafael Rojano Cáceres

The Basic Web Server CGI. CGI: Illustration. Web based Applications, Tomcat and Servlets - Lab 3 - CMPUT 391 Database Management Systems 4

UIMA Simple Server User Guide

3. The pool should be added now. You can start Weblogic server and see if there s any error message.

Servlet. Web Server. Servlets are modules of Java code that run in web server. Internet Explorer. Servlet. Fire Fox. Servlet.

Supplement IV.E: Tutorial for Tomcat For Introduction to Java Programming By Y. Daniel Liang

INTRODUCTION TO SERVLETS AND WEB CONTAINERS. Actions in Accord with All the Laws of Nature

2. Follow the installation directions and install the server on ccc. 3. We will call the root of your installation as $TOMCAT_DIR

Handout 31 Web Design & Development

Lab session Google Application Engine - GAE. Navid Nikaein

Servlet 5.1 JDBC 5.2 JDBC

Servlet Fudamentals. Celsina Bignoli

Stateless -Session Bean

Java Enterprise Edition. Java EE Oct Dec 2016 EFREI/M1 Jacques André Augustin Page 1

Chapter 17. Web-Application Development

Front-end / Back-end. How does your application communicate with services?

CS506 Web Design & Development Final Term Solved MCQs with Reference

APIs - what are they, really? Web API, Programming libraries, third party APIs etc

Servlet Basics. Agenda

AJP. CHAPTER 5: SERVLET -20 marks

Java Servlets. Preparing your System

Input from Files. Buffered Reader

Scheme G Sample Question Paper Unit Test 2

INTERNET PROGRAMMING TEST-3 SCHEME OF EVALUATION 1.A 3 LIFE CYCLE METHODS - 3M 1.B HTML FORM CREATION - 2 M

Unit 4 - Servlet. Servlet. Advantage of Servlet

JdbcResultSet.java. import java.sql.*;

Chettinad College of Engineering and Technology CHETTINAD COLLEGE OF ENGINEERING AND TECHNOLOGY DEPARTMENT OF COMPUTER SCIENCE AND TECHNOLOGY

Servlets. An extension of a web server runs inside a servlet container

Using the Jersey JAX-RS Reference Implementation 11g Release 1 (10.3.6)

HTTP status codes. Setting status of an HTTPServletResponse

"protocol stack! chat. Java

JAVA SERVLET. Server-side Programming ADVANCED FEATURES

Enterprise Java Technologies (Part 1 of 3) Component Architecture. Overview of Java EE. Java Servlets

Université Antonine - Baabda

SWE642 Oct. 22, 2003

HTTP. HTTP HTML Parsing. Java

Tutorial: Developing a Simple Hello World Portlet

Database Systems Lab. 11. JSP I 충남대학교컴퓨터공학과 데이타베이스시스템연구실

Kamnoetvidya Science Academy. Object Oriented Programming using Java. Ferdin Joe John Joseph. Java Session

Using Java servlets to generate dynamic WAP content

LearningPatterns, Inc. Courseware Student Guide

To create a view for students, staffs and courses in your departments using servlet/jsp.

LAB 1 PREPARED BY : DR. AJUNE WANIS ISMAIL FACULTY OF COMPUTING UNIVERSITI TEKNOLOGI MALAYSIA

JavaServer Pages (JSP)

COMP 213. Advanced Object-oriented Programming. Lecture 20. Network Programming

Networking Basics. network communication.

URL Kullanımı Get URL

Unit-4: Servlet Sessions:

TCP connections. Fundamentals of Internet Connections Objectives. Connect to an Echo port. java.net.socket

Allenhouse Institute of Technology (UPTU Code : 505) OOT Notes By Hammad Lari for B.Tech CSE V th Sem

Complimentary material for the book Software Engineering in the Agile World

&' () - #-& -#-!& 2 - % (3" 3 !!! + #%!%,)& ! "# * +,

CSET 4100 Assignment 3

Java servlets CSCI 470: Web Science Keith Vertanen Copyright 2013

CREATE A SERVLET PROGRAM TO DISPLAY THE STUDENTS MARKS. To create a servlet program to display the students marks

Servlets Basic Operations

Database Applications Recitation 6. Project 3: CMUQFlix CMUQ s Movies Recommendation System

Chapter 2 How to structure a web application with the MVC pattern

Accessing EJB in Web applications

Advanced Topics in Operating Systems. Manual for Lab Practices. Enterprise JavaBeans

This tutorial will teach you how to use Java Servlets to develop your web based applications in simple and easy steps.

Welcome To PhillyJUG. 6:30-7:00 pm - Network, eat, find a seat 7:00-7:15 pm - Brief announcements 7:15-8:30 pm - Tom Janofsky's presentation

To follow the Deitel publishing program, sign-up now for the DEITEL BUZZ ON-

Chapter #1. Program to demonstrate applet life cycle

Author - Ashfaque Ahmed

Network Programming. Powered by Pentalog. by Vlad Costel Ungureanu for Learn Stuff

Networking Code CSCI 201 Principles of Software Development

Enterprise Development

CreateServlet.java

Berner Fachhochschule Haute cole spcialise bernoise Berne University of Applied Sciences 2

Lab 10. Google App Engine. Tomas Lampo. November 11, 2010

CS 112 Introduction to Programming

Watch Core Java and Advanced Java Demo Video Here:

Java4570: Session Tracking using Cookies *

3.2 Example Configuration

Servlets and JSP (Java Server Pages)

Advanced Web Technology

UNIT-V. Web Servers: Tomcat Server Installation:

Introduction. This course Software Architecture with Java will discuss the following topics:

Java Servlets Basic Concepts and Programming

Java Card 3 Platform. Peter Allenbach Sun Microsystems, Inc.

Network. Dr. Jens Bennedsen, Aarhus University, School of Engineering Aarhus, Denmark

Applet. 1. init (): called once by the applet containers when an applet is loaded for execution.

F I N A L E X A M I N A T I O N

Introduction. Literature: Steelman & Murach, Murach s Java Servlets and JSP. Mike Murach & Associates Inc, 2003

The Structure and Components of

Transcription:

Getting started with Winstone Minimal servlet container

What is Winstone? Winstone is a small servlet container, consisting of a single JAR file. You can run Winstone on your computer using Java, and get a Servlet up and running very fast. Running Winstone will start a server listening on port 8080 on localhost, which will forward requests to servlets, according to your settings and configuration. https://sourceforge.net/projects/winstone/files/latest/download?source=files

Small example $ java -jar winstone-0.9.10.jar --webroot webroot [Winstone 2017/01/04 17:16:37] - AJP13 Listener started: port=8009 [Winstone 2017/01/04 17:16:37] - HTTP Listener started: port=8080 [Winstone 2017/01/04 17:16:37] - Winstone Servlet Engine v0.9.10 running: controlport=disabled

The directory layout. -- webroot `-- WEB-INF -- classes -- ExampleServlet.class `-- ExampleServlet.java -- lib `-- web.xml `-- winstone-0.9.10.jar $ java -jar winstone-0.9.10.jar --webroot webroot

The web.xml configuration file. -- webroot `-- WEB-INF -- classes -- ExampleServlet.class `-- ExampleServlet.java -- lib `-- web.xml `-- winstone-0.9.10.jar $ java -jar winstone-0.9.10.jar --webroot webroot

The web.xml configuration file - name and class $ cat webroot/web-inf/web.xml <?xml version="1.0" encoding="utf-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>example</servlet-name> <servlet-class>exampleservlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>example</servlet-name> <url-pattern>/example-servlet</url-pattern> </servlet-mapping> </web-app>

The web.xml configuration file - mapping $ cat webroot/web-inf/web.xml <?xml version="1.0" encoding="utf-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>example</servlet-name> <servlet-class>exampleservlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>example</servlet-name> <url-pattern>/example-servlet</url-pattern> </servlet-mapping> </web-app>

The web.xml configuration file - URL pattern $ cat webroot/web-inf/web.xml <?xml version="1.0" encoding="utf-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>example</servlet-name> <servlet-class>exampleservlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>example</servlet-name> <url-pattern>/example-servlet</url-pattern> </servlet-mapping> </web-app>

Mapping the request to the servlet <url-pattern>/example-servlet</url-pattern>

The example servlet import javax.servlet.*; import javax.servlet.http.*; import java.io.printwriter; import java.io.outputstreamwriter; import static java.nio.charset.standardcharsets.utf_8; import java.io.ioexception; public class ExampleServlet extends HttpServlet{ @Override public void doget(httpservletrequest request, HttpServletResponse response) throws ServletException, IOException{ PrintWriter out = new PrintWriter(new OutputStreamWriter(response.getOutputStream(), UTF_8), true); out.println("<html><head><title>example servlet</title></head>"); out.println("<body><p>hello from a servlet!</body>"); out.println("</html>"); out.close();

Compiling the servlet You need to have the servlet-api jar file on your class path. http://central.maven.org/maven2/javax/servlet/javax.servlet-api/3.1.0/javax.servlet-api-3.1.0.jar $ wget http://central.maven.org/maven2/javax/servlet/javax.servlet-api/3.1.0/javax.servlet-api-3.1.0.jar -O servlet-api-3.1.jar $ javac -cp.:servlet-api-3.1.jar webroot/web-inf/classes/exampleservlet.java

Using SQLite from a servlet As expected, you ll need to have the JAR file from xerial with the JDBC driver for SQLite if you use any of the classes from the org.sqlite packages, when you compile. But more specifically, you ll need to have the JAR on your class path when you run the servlet. So how does Winstone find the JAR file? It s simple. Put it in the webroot/web-inf/lib/ directory.

Example with a small student database. -- servlet-api-3.1.jar -- students.db -- webroot `-- WEB-INF -- classes -- DBServlet.class -- DBServlet.java -- ExampleServlet.class `-- ExampleServlet.java -- lib `-- sqlite-jdbc-3.8.11.2.jar `-- web.xml `-- winstone-0.9.10.jar

The database and web.xml $ echo '.schema students' sqlite3 students.db CREATE TABLE students(id integer primary key not null, name text not null); <servlet> <servlet-name>dbexample</servlet-name> <servlet-class>dbservlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>dbexample</servlet-name> <url-pattern>/database-servlet</url-pattern> </servlet-mapping>

Servlet source code - init() method public class DBServlet extends HttpServlet{ public void init() throws ServletException { // called only once try{ Class.forName("org.sqlite.JDBC"); catch(classnotfoundexception cnfe){ System.err.println("Could not load driver: "+cnfe.getmessage()); https://www.tutorialspoint.com/servlets/servlets-life-cycle.htm http://docs.oracle.com/javaee/5/tutorial/doc/bnafu.html

Servlet source code - doget() method public void doget(httpservletrequest request, HttpServletResponse response) throws ServletException, IOException{ PrintWriter out=new PrintWriter(new OutputStreamWriter(response.getOutputStream(), UTF_8), true); out.println("<html><head><title>example servlet</title></head><body>"); try{ Connection con = DriverManager.getConnection("jdbc:sqlite:students.db"); Statement stm = con.createstatement(); ResultSet rs = stm.executequery("select name FROM students"); while(rs.next()){ out.println(rs.getstring("name") + "<br />"); catch(sqlexception sqle){ out.println("database error: " + sqle.getmessage()); out.println("</body></html>"); out.close();

Servlet import statements import javax.servlet.*; import javax.servlet.http.*; import java.io.printwriter; import java.io.outputstreamwriter; import static java.nio.charset.standardcharsets.utf_8; import java.io.ioexception; import java.sql.*;

Bonus - calling the Servlet from Java $ java HTTPClient http://localhost:8080/database-servlet <html><head><title>example servlet</title></head> <body> Anna Andersson<br /> Beata Bengtssson<br /> Cecilia Carlsson<br /> David Davidsson<br /> Erik Eskilsson<br /> Fader Fourah<br /> </body> </html>

HTTPClient.java import java.io.*; import java.net.*; public class HTTPClient{ public static void main(string[] args){ HTTPClient client = new HTTPClient(); try{ client.getfile(args[0]); catch(ioexception ioe){ioe.printstacktrace(); public void getfile(string addr) throws IOException { URL url = new URL(addr); HttpURLConnection connection = (HttpURLConnection) url.openconnection(); connection.setrequestmethod("get"); BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; while ((line = in.readline())!= null) { System.out.println(line); in.close();

HTTPClient.java - The getfile() method public void getfile(string addr) throws IOException { URL url = new URL(addr); HttpURLConnection connection = (HttpURLConnection) url.openconnection(); connection.setrequestmethod("get"); BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; while ((line = in.readline())!= null) { System.out.println(line); in.close();

Show me the code! https://github.com/progund/java-web