Main.java:

package ru.Mark; import java.io.IOException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class Main extends HttpServlet{ /** * */ private static final long serialVersionUID = 1L; @Override public void doPost(HttpServletRequest req,HttpServletResponse resp) throws IOException, ServletException{ resp.setContentType("text/html"); String name=req.getParameter("login"); String password=req.getParameter("pass"); req.setAttribute("login", name); req.setAttribute("pass", password); RequestDispatcher disp=getServletContext().getRequestDispatcher("index.jsp"); disp.forward(req, resp); } } 

index.jsp:

 <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Page!</title> </head> <body> <h2>First page!</h2> <form action="index.jsp" method="post"> Login:<input type="text" name="login"><br> Password<input type="text" name="pass"><br> <input type="submit" name="send"><br> </form> login: ${login}<br> password: ${pass}<br> </body> </html> 

Why login and password is not displayed on the index page?

  • one
    I vote for the closure of this issue as not relevant to the topic, because it is SO in Russia, so you should ask in russian - Yuriy SPb
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky
  • one
    I vote for a ban of comments like "this is SO in Russia, so you should ask in russian" because they are English in Russian. also, gentlemen, are we here about help or snobbery, without wanting to teach, but stupid to punish? - ivanenok
  • It would be desirable to look at web.xml , and do not say that you do not have it. - Roman C

2 answers 2

instead of this code:

  RequestDispatcher disp=getServletContext().getRequestDispatcher("index.jsp"); disp.forward(req, resp); 

Try this one:

 req.getRequestDispatcher("index.jsp").forward(req, resp); 
  • And what is the actual difference? - Roman C
  • I did not ask how the dispatcher works, read the question carefully before answering. - Roman C

I had the same problem and it was decided to replace jsp page. If you have this index.jsp, then this is some kind of магия but Java does not lament these values ​​in the index page. For the sake of example, you can try to create another jsp, but which will do the same thing, only that it was not a main page.

That's actually my question regarding the same problem and in the comments of his "crutch" solution.