Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Calling a servlet from a JSP

843840Nov 1 2009 — edited Nov 2 2009
Hello. I'm not sure if this goes in the servlet section, but it seemed like it was more appropriate for here. I'm trying to write a JSP that functions like a comment page. The user fills out the form that has a place for his name and comments, and the JSP sends that information to a servlet that saves that information along with the current date, then calls the same JSP, which displays the new comments in the browser window. I see no evidence that the servlet is even being called. Can anyone help me? Here is the code.

CommentPage.jsp (in /ROOT)
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="java.io.*" %>

<html>
	<head>
		<title>Granada High School Class of 1986 Comment Page</title>
	</head>
	<body>
		<center><h1>Comment Page</h1></center>
		<p align="justify">
		This page is a place where you can share memories from high school, what you're doing these days, comments about this web site (good or bad), etc.
		</p>
		<form name="comment" action="comments.ProcessComments" method="post">
			<table>
				<tr>
					<td><b>Name:</b></td>
					<td><input type="text" name="name"></td>
				</tr>
				<tr>
					<td><b>Message:</b></td>
					<td><textarea name="message" rows="5" cols="50"></textarea></td>
				</tr>
				<tr>
					<td><input type="button" name="submit" value="Submit" onClick=""></td>
					<td>&nbsp;</td>
				</tr>
			</table>
		</form>
		<br>
		<%
			File input = new File("/comments.txt");
			if (input.exists()) {
				BufferedReader in = new BufferedReader(new FileReader(input));
				String output = null;

		%>
			<table>
        			<% while ((output = in.readLine()) != null) { %>
		  			<tr>
						<td><b>Name:</b></td>
						<td><% out.println(output); %></td>
					</tr>
					<tr>
						<td><b>Date:</b></td>
						<td>
						<% output = in.readLine();
						   out.println(output); %>
						</td>
					</tr>
					<tr>
						<td><b>Comments:</b></td>
						<td>
						<% output = in .readLine();
						   out.println(output); %>
						</td>
					</tr>
				<% } %>
			</table>
		<% } %>
	</body>
</html>
ProcessComments (in /ROOT/WEB-INF/classes/comments)
package comments;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

public class ProcessComments extends HttpServlet {
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws 		IOException, ServletException {

		// Get the name and message from the form
		String name = request.getParameter("name");
		String message = request.getParameter("message");

		// Get today's date to be added to the information from the form
		Calendar calendar = Calendar.getInstance();
		String date = "";
		date += getMonth(calendar) + calendar.get(Calendar.DAY_OF_MONTH) + ", " + calendar.get(Calendar.YEAR);

		// Write information to the file
		FileWriter output = new FileWriter("/comments.txt");
		BufferedWriter out = new BufferedWriter(output);
		out.write(name);
		out.write(date);
		out.write(message);
		out.close();

		// Redirect back to the JSP
		response.sendRedirect("/CommentPage.jsp");
	}

	public String getMonth(Calendar calendar) {
		switch(calendar.get(Calendar.MONTH)) {
			case Calendar.JANUARY: 		return "January ";
			case Calendar.FEBRUARY:		return "February ";
			case Calendar.MARCH:		return "March ";
			case Calendar.APRIL:		return "April ";
			case Calendar.MAY:		return "May ";
			case Calendar.JUNE: 		return "June ";
			case Calendar.JULY:		return "July ";
			case Calendar.AUGUST:		return "August ";
			case Calendar.SEPTEMBER:	return "September ";
			case Calendar.OCTOBER:		return "October ";
			case Calendar.NOVEMBER:		return "November ";
			case Calendar.DECEMBER:		return "January ";
			default:				return "";
		}
	}
}
web.xml (in /ROOT/WEB-INF)
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE web-app
  PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
	<welcome-file>home.jsp</welcome-file>
	<servlet>
		<servlet-name>CommentPageProcessor</servlet-name>
		<servlet-class>comments.ProcessComments</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>CommentPageProcessor</servlet-name>
		<url-pattern>/ProcessComments.do</url-pattern>
	</servlet-mapping>
</web-app>
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 30 2009
Added on Nov 1 2009
8 comments
690 views