0
votes

I want to set the context root, so that after I type in my browser IP:8080 my tomcat site must show my application. Now I create index.jsp in /webapps/ROOT and type in this file:

< %@ page import="java.util.List" %>

< %@ page  import="package1.Aplikacja" %>

in /webapps/ROOT/WEB-INF/ I have file: web.xml in /webapps/ROOT/WEB-INF/classes/package1 I have files: Aplikacja.class plikacja.java

So how can I now run this application on my IP:8080 page? I have imported the class, but how can I run it?

1
never System.exit(1); !!! use response.sendError() instead. - Zielu

1 Answers

0
votes

You need to define mapping between your servlet (Aplikacja) and urls in your web.xml

For example:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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">
    <display-name>Twoja App</display-name>
    <servlet>
        <servlet-name>Aplikacja</servlet-name>
        <servlet-class>package1.Aplikacja</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Aplikacja</servlet-name>
        <url-pattern>/app</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
</web-app>

and you can access your servlet by:

localhost:8080/app

(if you placed all the files correctly under the ROOT folder).

If you change servlet-mapping to * all the request will be handled by your Aplikacja

Normally you don't want all the requests going through one servlet, in such a case you define normal mapping ie /app and create index.html which redirects to the "default" servlet:

<html>
<head>
    <META HTTP-EQUIV="Refresh" CONTENT="0;URL=/app">
 </head>
</html>

You should probably use some IDE to build a proper webapp which is bundled into a .war file instead of placing the files manually under tomcat.