Hello I have an Embedded Tomcat java application. The tomcat starts and the index.jsp is displayed.
Now i have an An ajax Request which looks like this:
function auth(){
username = document.getElementById('login_username').value;
password = document.getElementById('login_pw').value;
if(username.length > 0 && password.length > 0){
$.ajax({
method: "POST",
url: '/login',
data: 'username=' + username + '&password=' + password
}).done(function(response){
console.log(response);
response = response.split(',');
if(response.length == 2){
window.location = response[1];
}
});
}
}
The auth() works. But every time i get a 404 error.
The servlet which should get the Request looks like this:
@WebServlet(
name="login",
urlPatterns = {"/login","sites/login","login"}
)
public class LoginServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
private ServletContext context;
public LoginServlet(){
super();
}
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Log.log("get");
resp.setContentType("text/html");
PrintWriter out=resp.getWriter();
Page p = new Page();
p.getBody().setContent("GET");
out.print(p.create());
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Log.log("Post");
resp.setContentType("text/html");
PrintWriter out=resp.getWriter();
Page p = new Page();
p.getBody().setContent("POST");
out.print(p.create());
}
public void init(ServletConfig config)throws ServletException{
this.context = config.getServletContext();
Log.log("Login servlet Initialized");
}
}
My question is, why is this not working. Have i to add The servlett to the Tomcat with the tomcat.addServlet(string,string,string) method?;
It works if i create a login.jsp in the base folder of my webapp and if i change the url of the ajax request to login.jsp.
Thanks