Servlet not showing umlauts? what should I do?
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
String myname = request.getParameter("benutzer");
String mypass = request.getParameter("pass");
PrintWriter myaus = response.getWriter();
myaus.println("Deine Benutzername und pass ist : "+myname+ " :: "+mypass);
}
I would also like a sentence with an umlaut to be Displayer.
Return a proper minimal HTML 5 page. Like this example.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>title goës here</title>
</head>
<body>
<p>Côntënt goës hérè</p>
</body>
</html>
Change en to your human language of choice.
I am trying to build a web application using servlet but facing this problem where i have just set my data in servlet class and fectching it in the jsp using ${}
JSP --->
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<c:out value="${data}"/>
<%= request.getAttribute("data") %>
</body>
</html>
Servlet class:
public class Naveen extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Object data = "Some data, can be a String or a Javabean";
request.setAttribute( "data", data );
RequestDispatcher rd = request.getRequestDispatcher( "/new.jsp" );
rd.forward( request, response );
response.getWriter().append( "Served at:" ).append( request.getContextPath() );
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet( request, response );
}
}
You need to use requestScope in c:out
<c:out value="${requestScope.data}">
For detailed example you can refer: https://www.journaldev.com/2090/jstl-tutorial-jstl-tags-example
I think you are directly calling /new.jsp!
For getting value you need to call the servlet URL.
That will fix your problem because on servlet request is despatched to /new.jsp with attribute value data.
Try this in your jsp file: Adding isELIgnored=false
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" isELIgnored="false" %>
This attribute is supposed to be on by default.You can add it to every jsp page to make EL(Expression Language) process successfully.
I am trying to pass a JsonObject to jsp. However, I think I cannot get the JsonObject using getAttribute method. How should I do to make this available.
There's a Servlet code, below.
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
#WebServlet("/ShapeRendererFileManager")
public class ShapeRendererFileManager extends HttpServlet {
private static final long serialVersionUID = 1L;
HttpSession session;
//Send Json to jsp
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=euc-kr");
session = request.getSession(true);
//System.out.println(request.getParameter("tmp").toString());
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("name", "jinny");
jsonObject.addProperty("title", "web");
session.setAttribute("jsonObject", jsonObject);
response.sendRedirect("index.jsp");
}
}
There's a jsp code below.
<%#page import="com.google.gson.JsonObject"%>
<%#page import="com.google.gson.JsonElement"%>
<%#page import="com.google.gson.JsonArray"%>
<!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=EUC-KR">
<title>start page</title>
</head>
<%
request.setCharacterEncoding("euc-kr");
response.setContentType("text/html;charset=euc-kr");
String tmp = "";
JsonObject json = (JsonObject)session.getAttribute("jsonObject");
tmp = json.get("name").toString(); //error at this line
%>
<body>
<script>
$(function(){
document.getElementByName("formtag").action="ShapeRendererFileManager";
document.getElementById("formtag").submit();
})
</script>
<h1><%= tmp %></h1>
<form name="formtag" action="" method="post">
</form>
</body>
</html>
Thanks in advance.
Here just a sample by use google Gson library. you can download that lib here or here
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HashMap<Object, Object> obj=new HashMap<>();
obj.put("name", "Janny");
obj.put("title", "Coder");
String jsonObject=new Gson().toJson(obj);
System.out.println(jsonObject);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
session.setAttribute("jsonObject",jsonObject);
request.getRequestDispatcher("index.jsp").forward(request, response);
}
Anyway, you can use Ajax to loop data or use JSTL!
#The Neo Noir Develper
Even I changed redirection to forward It doesn't work and make same error.
Here's servlet code below.
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=euc-kr");
session = request.getSession(true);
//System.out.println(request.getParameter("tmp").toString());
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("name", "jinny");
jsonObject.addProperty("title", "web");
session.setAttribute("jsonObject", jsonObject);
RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
rd.forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=euc-kr");
session = request.getSession(true);
//System.out.println(request.getParameter("tmp").toString());
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("name", "jinny");
jsonObject.addProperty("title", "web");
session.setAttribute("jsonObject", jsonObject);
RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
rd.forward(request, response);
//response.sendRedirect("index.jsp");
}
There's jsp code below.
<%# page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR" %>
<%#page import="com.google.gson.JsonObject"%>
<%#page import="com.google.gson.JsonElement"%>
<%#page import="com.google.gson.JsonArray"%>
<!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=EUC-KR">
<title>start page</title>
</head>
<%
request.setCharacterEncoding("euc-kr");
response.setContentType("text/html;charset=euc-kr");
JsonObject json = (JsonObject)session.getAttribute("jsonObject");
String tmp = json.get("name").toString(); //error at this line
%>
<body>
<!-- <div id="main_page">
</div>
<script data-main="main" src="libs/require-2.1.11.js"></script> -->
<script>
$(function(){
document.getElementByName("formtag").method="POST";
document.getElementByName("formtag").action="ShapeRendererFileManager";
document.getElementById("formtag").submit();
});
</script>
<h1><%= tmp %></h1>
<form name="formtag" action="" method="post">
</form>
</body>
</html>
Could you please try to use the below code in your servlet instead of sendRedirect
RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
rd.forward(request, response);
and also check the jsonObject in your jsp for null either you are getting null or not from session attribute,
JsonObject json = (JsonObject)session.getAttribute("jsonObject");
after doing above changes if you will again get the error then please post the error stack.
Can you give a try to change the contect type of your response
response.setContentType("application/json");
There are multiple things wrong in your code. Apart from using scriptlets.
Anyhoo, first, are you sure your session is being carried over? I say this because you are using a redirect instead of a simple RequestDispatcher Forward. A redirect creates a new request.
I think you should set this object in request and use a RequestDispatcher. Similarly, on the JSP side, you should use the request object to get the attribute back.
Without much info of how the session is created, it would be hard to pinpoint whether the session is being maintained at all or not.
I'm trying to do a servlet filter for a JSP project. What I want to do is to disallow a user to go back to previous page once he logouts. I followed this tutorial:
Prevent user from seeing previously visited secured page after logout
So I have this java file as my Filter class (file name is LogoutFilter.java):
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class LogoutFilter implements Filter {
FilterConfig config;
#Override
public void destroy() {
// TODO Auto-generated method stub
}
#Override
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
// TODO Auto-generated method stub
HttpServletResponse hsr = (HttpServletResponse) res;
hsr.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
hsr.setHeader("Pragma", "no-cache"); // HTTP 1.0.
hsr.setDateHeader("Expires", 0); // Proxies
chain.doFilter(req, res);
}
#Override
public void init(FilterConfig config) throws ServletException {
// TODO Auto-generated method stub
this.config = config;
}
}
I've also added the filter entry in my web.xml page. And the filter is working as I've checked it but the back button is still taking it back to the previous page after logout.
Here is my logout page where "admin_name" is a variable which I've added to session attribute during login.
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# page session="false" %>
<!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>Insert title here</title>
</head>
<body>
<%
HttpSession session = request.getSession(false);
String admin_name = (String)session.getAttribute("admin_name");
session.invalidate();
admin_name="";
response.sendRedirect("admin_login.jsp");
%>
</body>
</html>
I can't understand what am I doing wrong.
I'm begginer in JSP and I have the following servlet:
#SuppressWarnings("serial")
public class HelloAppIgorServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
RequestDispatcher disp = req.getRequestDispatcher("/mainpage.jsp");
disp.forward(req, resp);
}
#Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
PrintWriter pw = resp.getWriter();
pw.print("Test");
pw.close();
}
}
and one JSP file called mainpage.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>Insert title here</title>
</head>
<body>
<p>Hello there!<p>
<form action="." method="post">
<input type="text" name="name1" />
<input type="submit" value="name2" />
</form>
</body>
</html>
The problem is that doPost() method doesn't work. It's redirecting me to index.html page when I click the button. I'm sure that problem is not in servlets, so where can it be?
You need to set the appropriate action in <form action="." method="post">. The action is the (relative) URL of the servlet that you defined via <servlet-mapping> in the web.xml.