For the past few months, I've been trying to figure out how servlet mapping
works (to no avail). I don't know what part of my brain is incapable of processing this concept, but I just can't seem to figure it out.
I've asked 4-5 similar questions, but all responses have been inconclusive...and I can't delete the posts.
I know that there are dozens, maybe hundreds of duplicates but I've looked through at least 30 of them and still cannot grasp the basic concept of servlet mapping. My directory structure is just TOO different.
Disclaimer: I'm using software from 10 years ago, against my will. The topics should still be (somewhat) relevant. I recently transitioned from Java SE7 to a much older version.
- J2SE 1.4.2
- Sun Application Server PE 8
- Sun Deploytool
- EJB 2.1
Error:
HTTP Status 404 - /send_message
type Status report
message /send_message
description The requested resource (/send_message) is not available.
Sun-Java-System/Application-Server
Application structure:
├───SendMail_app │ index.jsp │ SendMailApp.ear │ └───sendmail SendMail.class SendMail.java SendMailBean.class SendMailBean.java SendMailHome.class SendMailHome.java SendMailServlet.class SendMailServlet.java
Note: My instructor noted that the older versions of Java don't require META-INF
, or WEB-INF
to be in the directory structure explicitly. It wasn't my hapless decision. Supposedly this was the norm in 2005.
application.xml
<?xml version='1.0' encoding='UTF-8'?>
http://java.sun.com/xml/ns/j2ee/application_1_4.xsd"> Application description SendMailApp ejb-jar-ic.jar war-ic.war sendmail
web.xml
<?xml version='1.0' encoding='UTF-8'?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>WebApp</display-name>
<servlet>
<display-name>SendMailServlet</display-name>
<servlet-name>SendMailServlet</servlet-name>
<servlet-class>sendmail.SendMailServlet</servlet-class>
</servlet>
<servlet>
<display-name>index</display-name>
<servlet-name>index</servlet-name>
<jsp-file>/index.jsp</jsp-file>
</servlet>
<servlet-mapping>
<servlet-name>SendMailServlet</servlet-name>
<url-pattern>/send_message</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>index</servlet-name>
<url-pattern>/index</url-pattern>
</servlet-mapping>
</web-app>
sun-web.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sun-web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Application Server 8.1 Servlet 2.4//EN" "http://www.sun.com/software/appserver/dtds/sun-web-app_2_4-1.dtd">
<sun-web-app>
<context-root>sendmail</context-root>
<session-config>
<session-manager persistence-type="memory">
<manager-properties/>
<store-properties/>
</session-manager>
<session-properties/>
<cookie-properties/>
</session-config>
<cache enabled="true" max-entries="4096" timeout-in-seconds="30">
<default-helper/>
</cache>
<class-loader delegate="true"/>
<jsp-config/>
<parameter-encoding default-charset="UTF8"/>
</sun-web-app>
**EAR file: C:\ejb_apps\SendMail_app\SendMailApp.ear EAR Display Name: SendMailApp
├───META-INF │ application.xml │ sun-application.xml | sun-j2ee-ri.project │ └───ejb-jar-ic.jar |___war-ic.war
war-ic.war WAR Display Name: WebApp Context Root: sendmail
├───WEB-INF | │ classes | │ |____sendmail | |----lib | |----tags | |----wsdl | │----sun-j2ee-ri.project | |----sun-web.xml | |----web.xml | |____index.jsp
ejb-jar-ic.jar JAR Display Name: SendMailJAR
├───META-INF | |----wsdl | |----ejb-jar.xml | |----sun-ejb-jar.xml | │----sun-j2ee-ri.project | |____sendmail |----SendMail.class |----SendMailBean.class |----SendMailHome.class
Index.jsp
<%@ page import="sendmail.*" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>Hermod</title>
<link rel="stylesheet" href="master.css"/>
</head>
<body>
<section id="sendmail_form">
<form method="post" action="/sendmail/send_message">
<label>From</label><input type="email" name="from" size="32" maxlength="256"/><br>
<label>To</label><input type="email" name="from" size="32" maxlength="256"/><br>
<label>CC</label><input type="email" name="cc" size="32"/><br>
<label>BCC</label><input type="email" name="bcc" size="32"/><br>
<label>Subject</label><input type="text" name="subject" size="32" maxlength="256"/><br>
<label>Body</label><input type="text" name="body" size="32"/><br>
<button id="submit" type="submit">Submit</button>
<button id="clear" type="reset">Clear</button>
</form>
</section>
<section id="sendmail_result">
<!--${requestScope.message_status}-->
</section>
</body>
</html>
SendMailServlet.java
package sendmail;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.naming.InitialContext;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.ejb.CreateException;
import java.io.IOException;
public class SendMailServlet extends HttpServlet {
private ServletContext context = null;
public SendMailServlet(){
super();
}
public void init(ServletConfig config) throws ServletException {
context = config.getServletContext();
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException {
try {
Context initial = new InitialContext();
Object objref = initial.lookup("java:comp/env/ejb/SendMail");
SendMailHome home = (SendMailHome)PortableRemoteObject.narrow(objref, SendMailHome.class);
SendMail sendmail = home.create();
String status = sendmail.send(request);
request.setAttribute("message_status", "status");
RequestDispatcher RD = context.getRequestDispatcher("/index");
RD.forward(request, response);
} catch (ServletException SE){
//TODO: log exception
} catch (IOException IOE){
//TODO: log exception
} catch (CreateException CE){
//TODO: log exception
} catch (NamingException NE){
//TODO: log exception
} finally {
super.destroy();
}
}
}
Of course, the code is riddled with neophytic errors but semantically what I'm trying to do is there. It's wrong, but if you think of it abstractly it's not so bad.
*What's happening: *
- I've tried dozens of incorrect servlet mappings to try to call
SendMailServlet.java
fromindex.jsp
- They're horrifically, pathetically incorrect.
localhost:8080/sendmail
- JSP loaded properly
- On submit (to be forwarded to
/send_message
(i.e.sendmail.SendMailServlet
)):
HTTP Status 404 - /send_message
type Status report
message /send_message
description The requested resource (/send_message) is not available.
Sun-Java-System/Application-Server
I'm not sure how to enumerate 2 months of failure, but I've tried ~40-50 different servlet mappings.
Where did I fail as a programmer?