Здравствуйте. :)
Хочу передать POST атрибут из сервлета в JSP страницу. Без HTML форм.
Как это можно сделать? Два дня не могу найти ничего толкового.
Безуспешно пробовал передавать таким образом:
public class PassPost extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session;
String encoding;
String urlAddress;
URL url;
HttpURLConnection httpURLConnection;
StringBuilder sbContent;
DataOutputStream stream;
InputStream inputStream;
// Getting current session
session = request.getSession();
// Setting encoding to unicode
encoding = "UTF-8";
// Creating URL
urlAddress = "http://localhost:8080/page.jsp";
url = new URL(urlAddress);
// Http URL connection
httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setUseCaches(false);
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
// String builder
sbContent = new StringBuilder();
sbContent.append("numid=");
sbContent.append(URLEncoder.encode(session.getAttribute("numid").toString(), encoding));
// Data output stream
stream = new DataOutputStream(httpURLConnection.getOutputStream ());
stream.writeBytes(sbContent.toString());
stream.flush();
stream.close();
// Input stream
inputStream = httpURLConnection.getInputStream();
inputStream.close();
response.sendRedirect(urlAddress);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}