Answer the question
In order to leave comments, you need to log in
Why doesn't the value change after request on the jsp page?
The data after the select select sends to the ajax servlet. The servlet receives the correct request, updates the content of the html variable, but on the jsp page, the attribute value remains the same
@WebServlet( urlPatterns = {"/index.html", "/readServlet"})
public class ReadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public ReadServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Entity> entities = null;
String countStr = request.getParameter("count");
Count count = null;
int pageNumber = 0;
if(countStr!=null && !countStr.isEmpty()) {
System.out.println("count="+countStr);
count = Count.getValue(countStr);
}
if(count==null) {
count = Count.TEN;
}
try {
entities = Portal.getSystem().getNews(0, count);
pageNumber = Portal.getSystem().getCount() / count.getCount();
} catch (ClassNotFoundException | SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String html = "";
if(entities!=null) {
for (Entity e : entities) {
html += e.html();
}
}
request.setAttribute("html", html);
request.setAttribute("page", pageNumber);
request.getRequestDispatcher("index.jsp").forward(request, response);
}
@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
{
doGet(request, response);
}
}
<%--
Created by IntelliJ IDEA.
User: dmitry
Date: 2019-07-26
Time: 01:35
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=utf-8" language="java" %>
<html>
<head>
<title>Title</title>
<meta charset = "UTF-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
<h1>This is index.jsp</h1>
<form action="loadServlet" method="post" enctype="multipart/form-data">
Заголовок: <input type="text" name="title" /><br/>
Текст новости: <input type="text" name="body">
<input type="file" name="file" />
Описание картинки: <input type="text" name="description">
<input type="submit" value="upload" />
</form>
<select id="select" name="count">
<option value ="10">10</option>
<option value ="20">20</option>
<option value ="50">50</option>
</select>
<div><%=request.getAttribute("page")%></div>
<div><%=request.getAttribute("html")%></div>
<%--<img src="<%=request.getAttribute("path")%>" />
<%=request.getAttribute("title")%>
<%=request.getAttribute("body") %>
<%=request.getAttribute("description") %>--%>
</body>
<script src="options.js"></script>
</html>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question