I had to do a little data editing work, so I decided to create a simple Web application and proceed with the GUI. Since I use it personally, I don't have to worry about maintainability. Anyway, I just need to make it quickly. Since Eclipse was installed in the terminal, I will make a dynamic web application in Java with Eclipse. Create a project or Servlet from a template, and then code it.
However, when I try to access Java Servlet with ajax from jsp, it does not respond. This was a big problem.
<web-app>
<servlet>
<servlet-name>test</servlet-name>
<servlet-class>TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>
</web-app>
$ajax({
url:'/testapp/test',
type:'POST',
data:{
'param1':'aaa',
'param2':'bbb'
}
})
.done((data)=> {console.log("done!");})
.fail((data)=> {console.log("fail!");})
.always((data)=> {console.log("always!");})
@WebServlet("/TestServlet")
public class TestServlet extends HttpServlet {
public TestServlet() {...}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {...}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {...}
}
fail!
always!
I couldn't reach TestServlet and I have no idea what caused it.
So, when I looked closely, I was curious about the @WebServlet ("/ TestServlet ")
that was included when TestServlet.java was automatically generated.
Good grief. I searched.
[Specify URL pattern with @WebServlet] (https://code-examples-ja.hateblo.jp/entry/2015/10/22/%40WebServlet%E3%81%A7URL%E3%83%91%E3%82%BF%E3%83%BC%E3%83%B3%E3%82%92%E6%8C%87%E5%AE%9A)
Gorgeous. You don't have to write web.xml ...
I deleted the description of web.xml and changed the ajax url to '/ testapp / TestServlet'
and it worked without difficulty.
If you usually rely on frameworks and templates, you will become a fossil engineer. Daily study clearly appears in the development speed at the moment of emergency.
Recommended Posts