I have created a simple program by using JSP and Servlets. After all, i have set and mapped my servlet in web.xml like below. But i am getting blank page always.
<servlet>
<servlet-name>example</servlet-name>
<servlet-class>exampleServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>example</servlet-name>
<url-pattern>/exampleServlet</url-pattern>
</servlet-mapping>
My JSP file looks like this.
<html>
<head></head>
<body>
<form action ="exampleServlet" method="POST" enctype="multipart/form-data">
<table width="500" style="margin-top:100px;">
<tr>
<td>Subject</td>
<td><input type="text" name="subj" id="subj"/></td>
</tr>
<tr>
<td>Upload File</td>
<td><input type="file" name="upload_file" id="upload_file"/></td>
</tr>
<tr>
<td> </td>
<td></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="Upload" /></td>
</tr>
</table>
</form>
</body>
</html>
Any exampleServlet is,
import java.io.File;
import java.util.List;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
public class exampleServlet extends HttpServlet {
public void init() {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String sub = request.getParameter("subj");
System.out.println(sub);
}
}
My file Structure is,
JSP file --> tomcat/webapps/application/index.jsp
Servlet --> tomcat/webapps/application/WEB-INF/classes/exampleServlet.class
Where i went wrong? What is the mistake i have made? Can you please suggest me?
EDIT : I am posting my form elements to that servlet. By that time it passes the URL like this http://localhost:8080/application/exampleServlet
Everything is fine in your application. You are getting blank page because your doPost method doesn't do anything. It prints the value only to the console output.
Change it to for e.g.:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter writer = response.getWriter();
writer.print("something");
}
And then take a look if something appears in the browser.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With