umlaut is not shown java Servlet - java

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.

Related

Request a variable defined in jsp from servlet? [duplicate]

This question already has an answer here:
Authentication filter and servlet for login
(1 answer)
Closed 6 years ago.
The username of the person who logs in is present on the index page and the code works fine. But trying to access the variable username from the servlet is proving hard. Any ideas?
index.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title><%=request.getParameter("username")%> - Cloud</title>
<link rel="stylesheet" href="css/bootstrap.css">
</head>
<header class="page-header">
<% String username = request.getParameter("username");%> // How can i get this variable?
<h1 align="center" class="inline"><%=username%> - Files</h1>
</header>
<body>
Servlet.java
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
}
?
You have to call the servlet through the jsp page
How to call servlet through a JSP page
This is an official example from oracle of how to do that:
Jsp2Servlet.jsp
<HTML>
<HEAD> <TITLE> JSP Calling Servlet Demo </TITLE> </HEAD>
<BODY>
<!-- Forward processing to a servlet -->
<% request.setAttribute("empid", "1234"); %>
<jsp:include page="/servlet/MyServlet?user=Smith" flush="true"/>
</BODY>
</HTML>
MyServlet.java
public class MyServlet extends HttpServlet {
public void doGet (HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
PrintWriter out= response.getWriter();
out.println("<B><BR>User:" + request.getParameter("user"));
out.println(", Employee number:" + request.getAttribute("empid") + "</B>");
this.getServletContext().getRequestDispatcher("/jsp/welcome.jsp").
include(request, response);
}
}
welcome.jsp
<HTML>
<HEAD> <TITLE> The Welcome JSP </TITLE> </HEAD>
<BODY>
<H3> Welcome! </H3>
<P><B> Today is <%= new java.util.Date() %>. Have a nice day! </B></P>
</BODY>
</HTML>
The key is in the line:
<jsp:include page="/servlet/MyServlet?user=Smith" flush="true"/>
In this example a jsp call a servlet a that servlet call another jsp. The example is from this page:
https://docs.oracle.com/cd/A87860_01/doc/java.817/a83726/basics4.htm

Getting Error When sending data from JSP to Servlet

I am using Dynamic Web Project.
This is my JSP Code. I am trying to send Hello to servlet
<%# 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>
<jsp:include page="/servlet/ServletCode" flush="true" >
<jsp:param name="username" value="Hello" />
</jsp:include>
</body>
</html>
This is my Servlet File.
package pack.exp;
public class ServletCode extends HttpServlet
{
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
String output= request.getParameter("username");
System.out.println(output);
PrintWriter pw = response.getWriter();
pw.println("Hello " + output);
}
}
In my JSP File I am getting this compile time error on this line.
Fragment "/servlet/ServletCode" was not found at expected path /JSpServletCode/WebContent/servlet/ServletCode
Please Help me with this.
I think You have to map your servlet in the web.xml, and the servlet-url you have to provide in the page. Something like below whould work.
<jsp:include page="/ServletCode" flush="true" >
<servlet>
<servlet-name>Servlet1</servlet-name>
<servlet-path>pack.exp.ServletCode</servlet-path>
</servlet>
<servlet-mapping>
<servlet-name>Servlet1</servlet-name>
<url-pattern>/ServletCode</url-pattern>
</servlet-mapping>
UPDATE
This is working for me
SERVLET
#WebServlet("/ServletCode")
public class ServletCode extends HttpServlet {
private static final long serialVersionUID = 1L;
public ServletCode() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String output= request.getParameter("username");
System.out.println(output);
PrintWriter pw = response.getWriter();
pw.println("Hello " + output);
}
}
JSP
<body>
<jsp:include page="/ServletCode" flush="true">
<jsp:param name="username" value="Hello" />
</jsp:include>
</body>
this is model servlet page:
<servlet>
<servlet-name>registerServlet</servlet-name>
<servlet-class>com.example.RegisterServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>registerServlet</servlet-name>
<url-pattern>/register</url-pattern>
</servlet-mapping>
And you must change the form like this format:
<form action="register" method="post">
this is do Post method:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String uname = request.getParameter("uname");
PrintWriter pw = response.getWriter();
pw.println("Hello " + output);
System.out.println(output);
// ...
}

Result redirection not working with jsp

I'm getting a ping result in my servlet.I'm trying to redirect it to another jsp file.
the jsp file for output opens.But nothing shows in it.
This is my servlet main code
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String ip = request.getParameter("ip");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
// out.println("The ip address is:"+ip+"\n");
String result = pingTest(ip);
out.println(result);
String redirect = "Output.jsp";
RequestDispatcher view = request.getRequestDispatcher(redirect);//Is it good approach to redirect request in ajax based servlet?
view.forward(request, response);
}
This is my output.jsp page
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>Ping Check Result</title>
</head>
<body>
</body>
</html
Do I need to add anything in output.jsp?
In your servlet:
request.setAttribute("result", result);
request.getRequestDispatcher("/WEB-INF/Output.jsp").forward(request, response);
In your JSP:
<pre>The data from servlet: ${result}</pre>
your servlet must be :
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String ip = request.getParameter("ip");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("The ip address is:"+ip+"\n");
String result = pingTest(ip);
out.println(result);
RequestDispatcher view = request.getRequestDispatcher();
view.forward(request, response);
}

neglect it--------------Sending an array to servlet from jsp page and recieving it in servlet------------ neglect it

*
neglect it
*
I am lerner in JSP and java field. I am stuck in a problem. I hv mentioned my code below.
My JSP code:
<%# 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>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script>
$(document).ready(function() {
$("#save").click( function ()
{
var arrayxx= new Array(5);
arrayxx[0]=0;
arrayxx[1]=3;
arrayxx[2]=4;
arrayxx[3]=9;
$.get('Save1',{arrayX:arrayxx},function(responseJson)
{
} );
});
});
</script>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<input type="button" id="save" value="save" ></input>
</body>
</html>
My servlet:
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Save1 extends HttpServlet {
private static final long serialVersionUID = 1L;
public Save1() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("INSIDE SERVLET");
String [] yourList = request.getParameterValues("arrayX");
System.out.println(request.toString());
System.out.println(yourList[0]);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
I cant able to pass the array from JSP to servlrt.
Please help me with this. When i receive the array, i found that array does not contain any element.
Thanks in advance
As per your requirement I would suggest you to go for ajax like this way
$.ajax(
{
type: "POST",
url: "servlet", //Your full URL goes here
data: { parametername: parametervalue},
success: function(data, textStatus, jqXHR){
//alert(data);
}

doPost() of GAE servlet doesn't work

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.

Categories

Resources