I have created a web app using Servlet and JSP. My front page looks like this: https://i.stack.imgur.com/UCF8t.png
I have two JSP files where I want to redirect my web app based on the click. If user click on delete, then my web app redirects it to FrontPage.jsp and when user click on create when web app should redirect it to index.jsp. I have referred this link Double forward in one servlet and tried to use conditional statements which will forward to proper resource depending on the proper request. But when the user enters the collection name and click on create, I got an error which says:
java.lang.IllegalStateException: Cannot forward after response has been committed
This is my servlet:
@WebServlet(description = "My first collection servlet", urlPatterns = {
"/CollectionPath" })
public class Collection extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
String deleteFileName = request.getParameter("filename");
System.out.println("you clicked on " + deleteFileName);
File f = new File(
"C:/Apps/eclipse-jee-mars-2-win32-x86_64/eclipse/" + deleteFileName);
if (f.delete()) {
System.out.println(f + "is deleted");
} else {
System.out.println(f + "is not deleteds");
}
String CollectionName = request.getParameter("myCollectionName");
request.setAttribute("collectionName", CollectionName);
if (CollectionName == null) {
request.getRequestDispatcher("FrontPage.jsp").forward(request, response);
}
else if (CollectionName != null) {
request.getRequestDispatcher("index.jsp").forward(request, response);
}
String Pattern = request.getParameter("Pattern");
String NotPattern = request.getParameter("NotPattern");
String CollectionNameValue = request.getParameter("CollectionNameValue");
File file = new File(CollectionNameValue + ".xml");
file.createNewFile();
FileWriter writer = new FileWriter(file);
System.out.println(file.getAbsolutePath());
writer.write("<?xml version=\"1.0\"?><collection><includePatterns>"
+ Pattern + "</includePatterns><doNotIncludePatterns>" + NotPattern
+ "</doNotIncludePatterns></collection>");
writer.flush();
writer.close();
}
}
I was able to redirect to the next page if user click on delete, but not able to redirect when user click on Create. This is my FrontPage.jsp
<%@page import="java.io.File"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta charset="ISO-8859-1">
</head>
<body>
A collection is a subset of the complete index. For example, create a
marketing collection or an engineering collection to support searches
only in the marketing or engineering pages in your index. You specify
the contents of the collection using URL patterns. Create as many
collections as you need.
<br>
<br>
<b>Current Collections</b>
<table width="100%" border="1">
<%
File folder = new File("C:/Apps/eclipse-jee-mars-2-win32-x86_64/eclipse");
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
%>
<tr>
<%
if (listOfFiles[i].isFile()) {
%>
<td><%=listOfFiles[i].getName()%></td>
<td>
<form method="post" action='CollectionPath'>
<input type="submit" value="Delete"
onclick="return confirm('Are you sure you want to proceed?')">
<input type="hidden" value="<%=listOfFiles[i].getName()%>"
name="filename" />
</form>
</td>
<%
}
%>
</tr>
<%
}
%>
</table>
<br>
<title>Create New Collection</title>
<h> <b>Create New Collection</b></h>
<br> Collection Name:
<textarea name="myCollectionName" cols="10" rows="1"></textarea>
<br>
<br>
<form method="post" action='CollectionPath'>
<input type="submit" value="Create"
style="color: white; background: blue" />
</form>
</body>
</html>
This is my index.jsp
<%@page import="java.io.File"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta charset="ISO-8859-1">
</head>
<body>
A collection is a subset of the complete index. For example, create a
marketing collection or an engineering collection to support searches
only in the marketing or engineering pages in your index. You specify
the contents of the collection using URL patterns. Create as many
collections as you need.
<br>
<br>
<b>Current Collections</b>
<table width="100%" border="1">
<%
File folder = new File("C:/Apps/eclipse-jee-mars-2-win32-x86_64/eclipse");
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
%>
<tr>
<%
if (listOfFiles[i].isFile()) {
%>
<td><%=listOfFiles[i].getName()%></td>
<td>
<form method="post" action='CollectionPath'>
<input type="submit" value="Delete"
onclick="return confirm('Are you sure you want to proceed?')">
<input type="hidden" value="<%=listOfFiles[i].getName()%>"
name="filename" />
</form>
</td>
<%
}
%>
</tr>
<%
}
%>
</table>
<br>
<title>Create New Collection</title>
<h> <b>Create New Collection</b></h>
<br> Collection Name:
<textarea name="myCollectionName" cols="10" rows="1"></textarea>
<br>
<br>
<form method="post" action='CollectionPath'>
<input type="submit" value="Create"
style="color: white; background: blue" />
</form>
</body>
</html>
I am newbie to Servlet and JSP. I might be missing something obvious. Any help would be appreciated