I am a newbie in java web application. I am using java 1.8, tomcat server 9.0.4, and jersey library in IntelliJ. I want to have a simple web page that shows a table of the database content when the get method is called and also a restful API from the same address when the post method alongside its parameters is called in order to fetch specific data from the same table. This is my web.xml config:
<servlet>
<servlet-name>Test API</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>api</param-value>
</init-param>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Test API</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
and this is my project structure:
This is my API java code:
@Path("hello")
public class postRequest {
@POST
@Produces(MediaType.TEXT_PLAIN)
public String doPost(@QueryParam("name") String name){
return "Hello,I am " + name;
}
}
and this is my web java code:
@WebServlet(name = "getRequest", urlPatterns = {"hello"})
public class getRequest extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
RequestDispatcher dispatcher = request.getRequestDispatcher("index.jsp");
dispatcher.forward(request, response);
}
}
when I call the address localhost/hello?name=hana in postman the result shows up but when I open the localhost/hello in the browser just a blank page comes. I tried to add another servlet and servlet-mapping without asterisks but the webserver does not start and triggers an error. How can I change web.xml to accomplish that?