I'm trying to handle alerts event with HtmlUnitDriver, but I have some problems, and I would like to understand why.
Here is the java code:
HtmlUnitDriver browser = new HtmlUnitDriver(true);
browser.get("http://localhost:8001/index.html");
browser.findElementById("myButton").click();
try {
WebDriverWait wait = new WebDriverWait(browser, 2);
wait.until(ExpectedConditions.alertIsPresent());
Alert alert = browser.switchTo().alert();
alert.accept();
System.out.println("ALERT");
} catch (Exception e) {
System.out.println(e.getMessage());
System.out.println("NO ALERT");
}
String htmlContent = browser.getPageSource();
System.out.println(htmlContent);
browser.close();
and this is the html code:
index.html
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<meta charset="utf-8" />
</head>
<body>
<form id="form1">
<input id="username" name="username" />
<button id="myButton" type="button" value="Page2">Go to Page2</button>
</form>
</body>
</html>
<script>
document.getElementById("myButton").onclick = function () {
location.href = "page2.html";
};
</script>
page2.html
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Page2</title>
<meta charset="utf-8" />
</head>
<body onload="myfunction('hello2')">
<p id="result"></p>
</body>
</html>
<script>
function myfunction(data) {
document.getElementById('result').innerHTML = data
}
</script>
The output in the console is :
NO ALERT
<?xml version="1.0" encoding="UTF-8"?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>
Page2
</title>
<meta charset="utf-8"/>
</head>
<body onload="myfunction('hello2')">
?
<p id="result">
hello2
</p>
<script>
//<![CDATA[
function myfunction(data) {
document.getElementById('result').innerHTML = data
}
//]]>
</script>
</body>
</html>
Looking at the output, it seems to be a little different from the source code, and about this I have few questions.
why the alert on page2.html isn't detected?
Why are there some extra characters, such as "?" and "// < ! [CDATA["? How can I avoid them?
I'm trying to handle alerts, and I'm at the beginning, so any suggestions will be appreciate.
Where are you expecting the alert to come from? The code for handling the alert looks okay, but there's nothing in the HTML that suggests that an alert will be triggered at any stage. The only script that runs on Page 2 is setting p#result's innerHTML to 'hello2'.
As far as the extra characters are concerned, I'm not so sure. This answer might shed some light on the CDATA stuff being generated.
Related
Here is my JSP Code
<%#page import="PresentationPkg.TestCls"%>
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form action="ex.jsp">
<input type="text" id="resultCode" name="resultCode">
<input type="button" name="viewValue" value="Submit View"/>
<%
String newTestResultCode = TestCls.getMaxTestResultID();
%>
</form>
</body>
</html>
I want to show variable 'newTestResultCode' value in 'resultCode' textBox by clicking button 'Submit View'
Try Below Code. Do not forget to add jquery js
< script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></ script>
Copy paste below code and run it.
<%#page import="PresentationPkg.TestCls"%>
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
String newTestResultCode = TestCls.getMaxTestResultID();
%>
<form action="ex.jsp">
<input type="text" id="resultCode" name="resultCode">
<input id="myButton" type="button" name="viewValue" value="Submit View" />
</form>
<script type="text/javascript">
$(document).ready(function() {
$('#myButton').click(function (){
$("#resultCode").val("<%=newTestResultCode%>");
});
});
</script>
</body>
</html>
You can also download jquery js in your project and use it. The js function will set your variable data into input box.
This should work:
<input type="text" id="resultCode" name="resultCode" value=<%=newTestResultCode%>>
I'm tryting to have email templates rendered using Thymeleaf and I would like to have the subject and the body in the same file, but rendered separately. I do not want to use spring view, just plain SpringTemplateEngine.
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<!--/*#thymesVar id="user" type="com.myapp.user.User"*/-->
<!--/*#thymesVar id="invitationId" type="java.util.UUID"*/-->
<head>
<title th:fragment="subject">Hello <span th:text="${user.name}"/></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<p>
Hello <span th:text="${user.name}"></span>,<br>
you've been invited to join MyApp.
</p>
<p>
Click on the following link to confirm your email and setup a password:<br>
<a th:href="#{https://myapp.com/accept-invitation(id=${invitationId})}">Accept invitation</a>
</p>
<p>
Best regards,<br/>
<em>The MyApp team</em>
</p>
</body>
</html>
There are several problems with this
First, I'm unable to get Thymeleaf render only contents of <title>
String subject = templateEngine.process(
templateFile,
ImmutableSet.of("subject"),
new Context(Locale.ENGLISH, contextVariables)
);
it renders literary only Hello - it looks like the opening <title> tag is always removed and the fragment is trimmer right after the first tag.
And when I render the whole template, it returns
<!DOCTYPE html>
<html>
<head>
Hello <span>pepa</span></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<p>
Hello <span>pepa</span>,<br>
...
Using the following in the <title> makes the output a bit nicer
<span th:text="${user.name}" th:remove="tag"></span>
but the opening tag is still missing.
When I would theoretically succeed in rendering the title correctly, I'd still have it wrapped in an HTML tag - which obviously cannot be used as an email subject. Is there a way to render only contents of the fragment?
I would really like to have both the email subject and body in the same template for cases, when for example I want to do some iteration in the title to generate it from a list - I do not want to do any string concatenations, I wanna have it in the template.
Any suggestions please?
If you want to include only the content of your fragment, assuming that your fragment is called your_fragment.html and is placed under /resources/templates/fragments you can do:
<head>
<title th:replace="fragments/your_fragment::your_fragment"/></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
And your_fragment.html:
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head></head>
<body>
<div th:fragment="your_fragment" th:remove="tag">
Hello <span th:text="${user.name}" th:remove="tag"></span>
</div>
</body>
</html>
This will render:
<head>
Hello username
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
This question already has answers here:
How should I use servlets and Ajax?
(7 answers)
Closed 7 years ago.
I'm writing a program that is working fine with the regular jsp-servlet interaction. But when I submit the same with ajasx, it is not working fine. Below is my code.
JSP-Servlet (w/o Ajax)
index.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>
<form name="f1" id="f1" method="post" action="Controller">
<input type="text" name="name1" id="name1" /> <input type="submit" />
</form>
</body>
</html>
Controller
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String nsme = request.getParameter("name1");
request.setAttribute("name", nsme);
request.getRequestDispatcher("index1.jsp").forward(request, response);
}
index1.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>
<input type="text" value="${name}" />
</body>
</html>
With Ajax
index.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>
<script type="text/javascript"
src="http://code.jquery.com/jquery-1.10.0.min.js"></script>
</head>
<body>
<form name="f1" id="f1">
<input type="text" name="name1" id="name1" /> <input type="submit"
name="x" id="x" />
</form>
<script type="text/javascript" src="SampleJS.js"></script>
</body>
</html>
SampleJS.js
$('#x').click(function() {
$.ajax({
type : 'POST',
url : 'Controller',
success : function(data) {
console.log(data)
},
});
});
Controller
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String nsme = request.getParameter("name1");
request.setAttribute("name", nsme);
request.getRequestDispatcher("index1.jsp").forward(request, response);
}
index1.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>
<input type="text" value="${name}" />
</body>
</html>
Here when I use the first method, The name is printed correctly. When I use it with AJAX, to my surprise nothing gets printed in the textbox.
Basically, We use request.getRequestDispatcher("GetCaseData.jsp").forward(request, response); to go to another page. What would be the alternative of it in my case?
please let me know where am I going wrong and how can I fix this.
In the Ajax case you don't send any form data. Use $.serialize to serialize the form data and send it to the server:
$('#x').click(function() {
var formData = $("#f1").serialize();
$.ajax({
type : 'POST',
url : 'Controller',
data : formData,
success : function(data) {
console.log(data)
},
});
});
After adding formData in your js file
Check your browser console
You can find your whole index1.jsp html content printed in console
with ${name} replaced with name you entered.
AJAX calls are advised to be used to stay in same page and update page content without page refresh after call.
In above code with AJAX, you are in index.jsp only even after AJAX call completion.
You can switch to other page (index1.jsp in your case) after AJAX call by using javascript window.location.href=<your file name >. However in your case, the page switches to index1.jsp but as new request. You can observe the same in your URL bar of browser. As you are storing the name attibute in request scope. You wont see the value you entered in name field of index1.jsp.
I am try to learn session implicit object. Please check. In index.jsp I create one form. On clicking submit button it will redirect to welcome.jsp. In welcome.jsp I am creating session attribute and in same page we redirect to second.jsp. In second.jsp we will get the name through session. But in welcom.jsp file is giving error. cannot find symbol: variable a location: class simplyfiedJSPServlet.
index.jsp
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1> This for getting session implicit object </h1>
<form action="welcome3.jsp">
<input type="text" name="uname">
<input type="submit" value="go"><br/>
</form>
</body>
</html>
welcome.jsp
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
String name=request.getParameter("uname");
out.print("Welcome"+name);
session.setAttribute("user", name);
<a href ="second.jsp" > second jsp </a>
%>
</body>
</html>
second.jsp
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
String name=(String )session.getAttribute("user");
%>
</body>
</html>
web.xml
<web-app>
<error-page>
<error-code> 500</error-code>
<location> /error.jsp</location>
</error-page>
</web-app>
You have html tag inside java code ,
<%
String name=request.getParameter("uname");
out.print("Welcome"+name);
session.setAttribute("user", name);
<a href ="second.jsp" > second jsp </a> // html anchor tag
%>
should be outside of it,
<a href ="second.jsp" > second jsp </a>
what are scriptlets in jsp?
So my setup for my web application is that I have a general header and footer and then I just include them in all my other pages so that all the common elements for all the pages are in a single page. The only problem I'm running into is when I want to redirect the user back to a login page if the "username" session has not already been created.
The problem that I"m running into is that I have an if statement at the top of my header.jsp and then I have the else in the footer.jsp and it is giving me a java syntax error. Any ideas? Here is the code I'm referring to...
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%
if(session.getAttribute("username") != null)
{
%>
<%# 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">
<!-- CSS files -->
<link rel="stylesheet" href="../CSS/headerStyle.css" type="text/css" media="screen" />
<title>Insert title here</title>
</head>
<body>
<div id="container">
<div id="header">
<div id="headerTitle">Title</div>
</div>
</div>
<%} %>
And then here is the footer
<!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">
<link rel="stylesheet" href="../CSS/headerStyle.css" type="text/css" media="screen" />
</head>
<body>
<div id="footer"></div>
</body>
</html>
<%
else
{
response.sendRedirect("../wa_login/login.jsp");
}
%>
However it is giving me an error on the else statement because the else doesn't have an if statement because it's in the header file.
Each .jsp has to compile as an independent unit. Hence the error.
Why not just perform the check in the header and redirect from there ?
<%
if(session.getAttribute("username") == null)
{
response.sendRedirect("../wa_login/login.jsp");
}
%>