Issue
I haven't changed anything in index.jsp and web.xml. The ouput has to be Hello World but there isn't any.
Here is the code of MyServlet.java:
public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter printWriter = response.getWriter();
printWriter.println("Hello World");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) {
}}
Solution
Here you need to call name which you mapped in web.xml
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>com.test.HelloWorld
</servlet-class>
<load-on-startup>1</load-on-startup> // this is optional
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
then you can access like http://localhost:8080/hello
If you are using below servlet 3.0 you need have mapping of your servlet as follows:
if you are using servlet 3.0 (annotation based) then it should be as follows:
@WebServlet("/hello")
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
PrintWriter writer = resp.getWriter();
writer.println("<p>Hello World!</p>");
} }
then you can access like http://localhost:8080/hello
Answered By - Satya Dev Addepally
Answer Checked By - Pedro (JavaFixing Volunteer)