I hope you can help handle problem with endless loop in servlet. Such problems usually causes wrong servlet-mapping (usualy it's "/*"). But in my case, it has specific value - name of conrete jsp file.
Servlet:
public class TrainsListServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void processRequest(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
EmployeeService employeeService = new EmployeeServiceImpl();
List<Train> trains = (List<Train>) employeeService.getTrains();
request.setAttribute("trains", trains);
RequestDispatcher dispatcher = getServletContext()
.getRequestDispatcher("/getTrainsList.jsp");
dispatcher.forward(request, response);
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
}
web.xml:
...
<servlet>
<servlet-name>TrainsList</servlet-name>
<display-name>TrainsList</display-name>
<description></description>
<servlet-class>ru.tsystems.jsproject.sbb.Servlets.TrainsListServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>TrainsList</servlet-name>
<url-pattern>/getTrainsList.jsp</url-pattern>
</servlet-mapping>
...
getTrainsList.jsp:
...
<c:forEach var="train" items="${trains}">
<tr>
<td><c:out value="${train.getNumber()}" /></td>
<td><c:out value="${train.getSeatsCount()}" /></td>
<td><c:out value="${train.getFrequence()}" /></td>
</tr>
</c:forEach>
...
it's all causes endless loop in processRequest method. Please help,
tell me what I am doing wrong?
Map it with html file
<servlet-name>TrainsList</servlet-name>
<url-pattern>/getTrainsList.html</url-pattern>
And follow http://localhost:8080/.../getTrainsList.html and you will get as response from Servlet getTrainsList.jsp
Related
First of all, please have mercy for this noobie question. I’m currently working on a web application project and stumbled across this basic problem I can’t figure out to fix.
Basically, I have the following:
index.html sending a POST request to servlet1.
servlet1 sending a redirect to the browser to servlet2.
servlet2 to return content.
I do see the content returned from servlet2 in my browser, but when I look into IE Developer Tools (F12), I see that the GET request to servlet2 never finishes (STATUS PENDING). There must be something very basic that I’m missing…
In below screen shot, "Ausstehend" translates to "pending".
IE Screenshot:
index.html:
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="servlet1" method="post">
<input type="text" name="test" placeholder="some data">
<button type="submit"><b>send</b></button>
</form>
</body>
</html>
servlet1:
public class servlet1 extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.sendRedirect("servlet2");
}
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
#Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
}
servlet2:
public class servlet2 extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet servlet2</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>HELLO</h1>");
out.println("</body>");
out.println("</html>");
out.close();
}
}
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
#Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
}
When I access "http://localhost:8080/project/Song", dont appears any error, it works it lists some songs in the page. When I click on a song I go to a URL "http://localhost:8080/SongPage?name=A+Tribe&id=201" but here it appears a 404 error, maybe because the url has parameters, accessing this URL appears an error:
HTTP Status 404 – Not Found
Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
Do you know why?
Maybe its because of this urlPattern below:
#WebServlet(name ="SongPage", urlPatterns = {"/SongPage"})
I have this servlet below:
#WebServlet(name ="Song", urlPatterns = {"/Song"})
public class Song extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
QueryManager qm = new QueryManager();
ArrayList<ArrayList<String>> result = qm.Songs();
qm.closeConnections();
request.setAttribute("result", result.get(0));
request.setAttribute("id", result.get(1));
RequestDispatcher view=request.getRequestDispatcher("songList.jsp");
view.forward(request,response);
}
}
In songList.jsp i have this:
<c:forEach items="${result}" var="item" varStatus="status">
${result[status.index]} <br />
</c:forEach>
SongPage.java:
#WebServlet(name ="SongPage", urlPatterns = {"/SongPage"})
public class SongPage extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name").replace("+"," ");
...
}
Your original url "http://localhost:8080/project/Song" has "project" as base path but the other url "http://localhost:8080/SongPage?name=A+Tribe&id=201" does not have that path. Most likely you need to update your JSP to include it in the "href" tag as a quick fix:
${result[status.index]} <br />
or most likely if it is actually a context path:
${result[status.index]} <br />
I have home page and when I click on reference Servlet don't work and I get error 404. I think issue in web.xml mapping, but a don't understand where. Please help me correct this issue. Thank you.
My web.xml
<!--Homepage.-->
<servlet>
<servlet-name>HomePageServlet</servlet-name>
<servlet-class>ru.pravvich.servlets.HomePageServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HomePageServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--Add user in database.-->
<servlet>
<servlet-name>AddUserServlet</servlet-name>
<servlet-class>ru.pravvich.servlets.AddUserServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AddUserServlet</servlet-name>
<url-pattern>/addition</url-pattern>
</servlet-mapping>
My jsp homepage:
<body>
<ul>
<li>addition</li>
</ul>
</body>
And servlet with doGet method for it:
public class HomePageServlet extends HttpServlet {
#Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("UTF8");
req.getRequestDispatcher("/WEB-INF/views/index.jsp").forward(req,resp);
}
}
And by http://localhost:8080/items/ I get my home page.
But, when I click on reference from index.jsp, return: HTTP Status [404] – [Not Found]
My addition.jsp same lie in /WEB-INF/views/addition.jsp
My Servlet for work with addition.jsp :
public class AddUserServlet extends HttpServlet {
private DBJoint db;
#Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
db = (DBJoint) getServletContext().getAttribute("db");
db.getDBExecutor().addUser(
new User(req.getParameter("name"),
req.getParameter("login"),
req.getParameter("email")));
req.setAttribute("serverAnswer", "Add ok!");
req.getRequestDispatcher("/WEB-INF/views/answer.jsp").forward(req, resp);
}
}
And addition.jsp:
<body>
<form method="post" action="addition">
<input type="text" required placeholder="name" name="name"><br>
<input type="text" required placeholder="login" name="login"><br>
<input type="text" required placeholder="email" name="email"><br>
<input type="submit" value="add">
</form>
</body>
I would suggest to use try/catch and debugger mode.
And try to use like this your getRequestDispatcher
request.getRequestDispatcher("answer.jsp").forward(request, response);
or
req.getRequestDispatcher("~/WEB-INF/views/answer.jsp").forward(req, resp);
I think you need to get parameter for each of one, and than set. Try this;
public class AddUserServlet extends HttpServlet {
private DBJoint db;
#Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
db = (DBJoint) getServletContext().getAttribute("db");
String Name = request.getParameter("name");
String Login= request.getParameter("login");
String Email= request.getParameter("email");
db.getDBExecutor().addUser(
new User(Name, Login, Email);
//And you need to 'serverAnswer' item in your 'answer.jsp' you know.
request.setAttribute("serverAnswer", "Add ok!");
request.getRequestDispatcher("answer.jsp").forward(req, resp);
}
}
And then you can use getAttribute like this in your answer.jsp,
<%String Answer= (String)request.getAttribute("serverAnswer"); %><%= Answer%>
Don't blame me, just i wanna help to you, i hope so it can be help to you, if you wanna you can look my trial project; https://github.com/anymaa/GNOHesap
Have a nice coding :)
I have a simple servlet app. In the servlet class, doPost() and doGet() basically get the same data from model class, however, doGet() cannot get any data.
public class ClickerServlet extends HttpServlet {
ClickerModel clickerModel = new ClickerModel();
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String servletPath = request.getServletPath();
if (servletPath.equals("/submit")) {
doPost(request, response);
} else if (servletPath.equals("/getResults")) {
doGet(request, response);
}
}
#Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String answer = request.getParameter("answer");
clickerModel.addAnswer(answer);
request.setAttribute("recentAnswer", clickerModel.getRecentAnswer());
request.getRequestDispatcher("submit.jsp").forward(request, response);
}
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("ok" + clickerModel.getRecentAnswer());
request.setAttribute("clickerModel", clickerModel);
request.getRequestDispatcher("results.jsp").forward(request, response);
}
}
My ClickerModel class:
public class ClickerModel {
private String recentAnswer;
private HashMap<String, Integer> answers;
public ClickerModel() {
recentAnswer = "";
answers = new HashMap<>();
}
public void addAnswer(String answer) {
recentAnswer = answer;
if (answers.containsKey(answer)) {
answers.put(answer, answers.get(answer) + 1);
} else {
answers.put(answer, 1);
}
}
public void clearAnswers() {
answers = new HashMap<>();
}
public HashMap<String, Integer> getAnswers() {
return answers;
}
public String getRecentAnswer() {
return recentAnswer;
}
}
Anyone knows why I cannot get any data from model using doGet()? Thanks!
For more information:
doPost() is called from a form submission.
<form action="submit" method="post">
<input type="radio" name="answer" value="A"> A<br>
<input type="radio" name="answer" value="B"> B<br>
<input type="radio" name="answer" value="C"> C<br>
<input type="radio" name="answer" value="D"> D<br>
<br><input type="submit" value="submit">
</form>
doGet() is called when I directly paste and hit the url (localhost:something/getResults) in the browser.
And my web.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<welcome-file-list>
<welcome-file>home.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Results</servlet-name>
<servlet-class>Clicker.ClickerServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Results</servlet-name>
<url-pattern>/getResults</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Submit</servlet-name>
<servlet-class>Clicker.ClickerServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Submit</servlet-name>
<url-pattern>/submit</url-pattern>
</servlet-mapping>
</web-app>
I my understanding, that you place the doGet() method in Results servlet, not same as Submit one.
Submit Servlet:
#Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String answer = request.getParameter("answer");
clickerModel.addAnswer(answer);
request.setAttribute("recentAnswer", clickerModel.getRecentAnswer());
//request.getRequestDispatcher("submit.jsp").forward(request, response);
response.sendRedirect("getResults"); // <---After submit will go to this page.
}
Results Servlet:
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("ok" + clickerModel.getRecentAnswer());
request.setAttribute("clickerModel", clickerModel);
request.getRequestDispatcher("results.jsp").forward(request, response);
}
This is my Login.jsp page
<form name="login" action="ValidateServlet" method="post">
<input type="hidden" name="pagename" value="login"/>
<table>
<tr>
<td><b>USERNAME:</b><input type="text" name="txtUsername"/> </td>
</tr>
<tr>
<td><b>PASSWORD:</b><input type="password" name="txtPassword"/></td>
</tr>
<tr>
<td><input type="SUBMIT" value="SIGN IN"/></td>
</tr>
<tr>
<td>Forgot Password</td>
</tr>
<tr>
<td> Create New user </td>
</tr>
</table>
</form>`
And this is my SignupServlet:
protected void doPost(HttpServletRequest request, HttpServletResponse response throws
ServletException, IOException {
RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/WebPages/Signup.jsp");
rd.forward(request, response);
}
}
and this is my web.xml
<servlet>
<servlet-name>SignupServlet</servlet-name>
<servlet-class>com.affiliate.servlet.SignupServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SignupServlet</servlet-name>
<url-pattern>/SignUp</url-pattern>
</servlet-mapping>
Login.jsp is in Webcontent/WEB-INF/WebPages/Login.jsp
and SignupServlet in JavaResources/src/com.affiliate.servlet/SignupServlet
but my Login is not redirecting to the SignupServlet. And i have doubt on my href and form action. Please help me in this regard.
Change
Create New user
↑
To
Create New user
And in anchor tag there is no post method available. You need to change the code in SignupServlet
protected void doGet(HttpServletRequest request, HttpServletResponse response throws
ServletException, IOException
{
RequestDispatcher rd = request.getRequestDispatcher("Signup.jsp");
rd.forward(request, response);
}
If you want post request using a tag then refer POST from an tag
replace
Forgot Password
Create New user
To
Forgot Password
Create New user
Remove "/" before Servlet URL Pattern
Change this code
protected void doPost(HttpServletRequest request, HttpServletResponse response throws
ServletException, IOException {
RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/WebPages/Signup.jsp");
rd.forward(request, response);
}
to
protected void doPost(HttpServletRequest request, HttpServletResponse response throws
ServletException, IOException {
getServletContext().getRequestDispatcher("WEB-INF/WebPages/Signup.jsp").forward(request, response);
}
In your login.jsp call SignUp servlet in following way
<td> Create New user </td>
and In SignUp servlet in your doGet method
RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/webpages/Signup.jsp");
rd.forward(request, response);
Because when you call a servlet from a link or href it the doGet method gets run. Hope this helps