In my Quarkus project, I am sending an qute template in an email using mail object.
The data is dynamically added to qute template from my code before sending the mail.
The sample template is
<html>
<head>
</head>
<body class="body">
<br>
Hi <b>Receiver</b>,
<br><br>
{body}
<br><br>
</body>
</html>
The email body is added from code using this
#Inject
#Location("sampleMail")
MailTemplate mailObject;
public void sendMail() {
String emailBody = "First line <br>" +
"Second line \n" +
"Third line \\n" + System.lineSeparator() +
"Fourth line";
mailObject.to(recipient)
.subject("Default subject")
.data("body", emailBody)
.send().subscribe().with(
success -> logger.info("Message sent"),
fail -> logger.error("Exception while sending mail", fail));
}
}
Even after trying <br>, \n, \\n, System.lineSeparator() in body string, the new line is not rendered in qute template in the html that is being sent in the mail. All lines are present in one single line, new line is not created. Have checked the quarkus guides but there was no mention about this.
Any solution or suggestions for solving this issue ?
I believe your problem is related to your values being treated as text by Qute when rendering your template.
The <br> should work if you replace your {body} by {body.raw}, like this :
<html>
<head>
</head>
<body class="body">
<br>
Hi <b>Receiver</b>,
<br><br>
{body.raw}
<br><br>
</body>
</html>
And of course :
String emailBody = "First line <br>" +
"Second line <br>" +
"Third line <br>" +
"Fourth line";
Related
I need to create web scraper utility which get web resources by URL. Then count number of provided word(s) occurrence on webpage and number of characters.
URL url = new URL(urlStr);
URLConnection connection = url.openConnection();
InputStream inputStream = connection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream,"UTF-8"));
With that I can get all text on page(and html tags) so what I do next?
Can someone help me with that? Some doc or sthg to read. I need use only JavaSE. Can't use 3d party library.
For example, you have page.html:
<!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>Login Page</title>
</head>
<body>
<div id="login" class="simple" >
<form action="login.do">
Username : <input id="username" type="text" />
Password : <input id="password" type="password" />
<input id="submit" type="submit" />
<input id="reset" type="reset" />
</form>
</div>
</body>
</html>
To parse it you can with:
import java.io.File;
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
/**
* Java Program to parse/read HTML documents from File using Jsoup library.
*/
public class HTMLParser{
public static void main(String args[]) {
// Parse HTML String using JSoup library
String HTMLSTring = "<!DOCTYPE html>"
+ "<html>"
+ "<head>"
+ "<title>JSoup Example</title>"
+ "</head>"
+ "<body>"
+ "<table><tr><td><h1>HelloWorld</h1></tr>"
+ "</table>"
+ "</body>"
+ "</html>";
Document html = Jsoup.parse(HTMLSTring);
String title = html.title();
String h1 = html.body().getElementsByTag("h1").text();
System.out.println("Input HTML String to JSoup :" + HTMLSTring);
System.out.println("After parsing, Title : " + title);
System.out.println("Afte parsing, Heading : " + h1);
// JSoup Example 2 - Reading HTML page from URL
Document doc;
try {
doc = Jsoup.connect("http://google.com/").get();
title = doc.title();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Jsoup Can read HTML page from URL, title : " + title);
// JSoup Example 3 - Parsing an HTML file in Java
//Document htmlFile = Jsoup.parse("login.html", "ISO-8859-1"); // wrong
Document htmlFile = null;
try {
htmlFile = Jsoup.parse(new File("login.html"), "ISO-8859-1");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} // right
title = htmlFile.title();
Element div = htmlFile.getElementById("login");
String cssClass = div.className(); // getting class form HTML element
System.out.println("Jsoup can also parse HTML file directly");
System.out.println("title : " + title);
System.out.println("class of div tag : " + cssClass);
}
}
Output:
Input HTML String to JSoup :<!DOCTYPE html><html><head><title>JSoup Example</title></head><body><table><tr><td><h1>HelloWorld</h1></tr></table></body></html>
After parsing, Title : JSoup Example
Afte parsing, Heading : HelloWorld
Jsoup Can read HTML page from URL, title : Google
Jsoup can also parse HTML file directly
title : Login Page
class of div tag : simple
I am developing a web site with jsp.
Like in php, I am trying to separate html and java code to check user and calling html content by java classes.
Here is my index.jsp page:
<!DOCTYPE html>
<html>
<head>
<%=DefaultTagContents.putHeader()%>
</head>
<body>
<%
if(request.getSession().getAttribute("username") == null){
out.print(DefaultTagContents.putUnRegisteredNavbar());
out.print(DefaultTagContents.putUnregisteredIndexContent());
}else{
out.print(DefaultTagContents.putRegisteredNavbar());
out.print(DefaultTagContents.putRegisteredIndexContent());
}
%>
</body>
</html>
But when I use classes like above; it becomes so confusing.Here is the java class "DefaultTagContents" :
public static String putRegisteredNavbar(){
return "<nav class=\"navbar navbar-default\">\n" +
" <div id=\"content-container\" class=\"container-fluid\">\n" +
"\n" +
" <ul class=\"nav navbar-nav\">\n" +
" <li>Ana Sayfa</li>\n" +
" <li>Profilim Ne Halde?</li>\n" +
" <li>Siz Kimsiniz?</li>\n" +
" </ul>\n" +
"\n" +
" <form class=\"navbar-form navbar-right\">\n" +
" <div class=\"input-group\">\n" +
" <input type=\"text\" class=\"form-control\" placeholder=\"Ne istedin?\">\n" +
" <div class=\"input-group-btn\">\n" +
" <div class=\"input-group-btn\">\n" +
" <button class=\"btn btn-default\" type=\"submit\">\n" +
" <i class=\"glyphicon glyphicon-search\"></i>\n" +
" </button>\n" +
" </div>\n" +
" </div>\n" +
" </div>\n" +
" </form>\n" +
" </div>\n" +
"</nav>";
}
This seems so complex to develop index page html.What should I do instead of using it like this?
The way you are doing it (and using java scriptlets in general) is definitely not recommended and should be avoided.
JSP pages are used as views in MVC pattern. They are meant for separating the concern of business logic from that of presentation.
In this case, you can put html code in separate files and include them according to the condition (whether username is set).
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<%=DefaultTagContents.putHeader()%>
</head>
<body>
<c:choose>
<c:when test="${sessionScope.username != null}">
<jsp:include page="RegisteredNavbar.html" />
<jsp:include page="RegisteredIndexContent.html" />
</c:when>
<c:otherwise>
<jsp:include page="UnRegisteredNavbar.html" />
<jsp:include page="UnRegisteredIndexContent.html" />
</c:otherwise>
</c:choose>
</body>
</html>
I want to pass a variable from HTML to Java. For this, I wrote the following code:
<!doctype html>
<html>
<title>How to create a typewriter or typing effect with jQuery</title>
<div id="example1">fsdfsdfojsdlk sdfj lskdhfk sdf </div>
<style>
body{
background: transparent;
color: #ec5a62;
}
#container{
font-size: 7em;
}
</style>
</head>
<body>
<div id="container"></div>
<!--
We use Google's CDN to serve the jQuery js libs.
To speed up the page load we put these scripts at the bottom of the page
-->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
<script>
//define text
var text = ("document.getElementById("example1")");
//text is split up to letters
$.each(text.split(''), function(i, letter){
//we add 100*i ms delay to each letter
setTimeout(function(){
//we add the letter to the container
$('#container').html($('#container').html() + letter);
}, 30*i);
});
</script>
</body>
</html>
But it is not working. How can I achieve this?
Please do help me.
I'm using var text =("document.getElementById("example1")");
But its not working.
to get value use var x=document.getElementById("example1").value;
your code should be like this:
var text=document.getElementById("example1").value;
//text is split up to letters
$.each(text.split(''), function(i, letter){
//we add 100*i ms delay to each letter
setTimeout(function(){
//we add the letter to the container
$('#container').html($('#container').html() + letter);
}, 30*i);
});
I am having trouble while writing to file using the PrintWriter. Following is my code:
String abc = request.getParameter("textAreaField"); //String is "a b c" (with spaces)
String fileA = dir + "/A";
PrintWriter fileWriterA = new PrintWriter(new FileOutputStream(fileA,true));
fileWriterA.println(abc);
fileWriterA.close();
The problem I am having here is while writing to the file "A" in the directory "dir" only "a" from String abc will be written and the rest after the space is not written. String abc here in the code is coming from a textarea in html and I have the above code in my servlet. I am not able to understand why it won't write the string with spaces to file. I think it should. I have also checked by printing the String abc and it does print the string "a b c" (with spaces). But it won't print that to file. Is there a problem with my code? Any help would be appreciated.
Thanks in advance.
I have used your code and written a servlet . It is working absolutely fine. Here is the code.
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println(request.getParameter("ta"));
String abc = request.getParameter("ta");
String fileA = "/A";
PrintWriter fileWriterA = new PrintWriter(new FileOutputStream(fileA,true));
fileWriterA.println(abc);
fileWriterA.close();
}
and here is the jsp:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="Test">
<textarea rows="20" cols="20" name="ta"></textarea><!-- having value -- check some spaces -->
<input type="submit" value="Submit">
</form>
</body>
</html>
Part of my homework for tomorrow is to search and add entries using Java EE. If the search is not existing, an add item option will show as follow:
Supposedly, when the Stock ID is not existing, It will be transfered to the Add Item Text Field of StockID. But I have no idea how to do it. My code is as follows:
Servlet:
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
Item item = (Item) request.getAttribute("invenItem");
if (item != null) {
out.println("<html><title>Inventory Item</title>");
out.println("<body><h1>Inventory Item Details:</h1>");
out.println("Stock ID : " + item.getStockID() + "<br/>");
out.println("Name : " + item.getItemName() + "<br/>");
out.println("Unit Price: " + item.getUnitPrice() + "<br/>");
out.println("On Stock : " + item.getOnStock() + "<br/>");
out.println("</body>");
out.println("</html>");
} else {
RequestDispatcher rd = request.getRequestDispatcher("/DataForm.html");
rd.include(request, response);
out.println("Sorry Item not found..");
rd = request.getRequestDispatcher("AddEntry.html");
rd.include(request, response);
}
}
}
HTML:
<html>
<head>
<title>Add Entry</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<h2>Add Item:</h2>
Stock ID: <input type ="text" name ="stockId" value="???"> <br> <--how to get it?
Item Name: <input type ="text" name ="name"> <br>
Unit Price: <input type ="text" name ="unitPrice"> <br>
On Stock : <input type ="text" name ="stock"> <br><br>
<input type ="submit" value ="Add Item">
</body>
</html>
You're approaching this the wrong way. HTML belongs in JSP files, not in Servlet classes. Also, EL ${} doesn't run in plain HTML files at all, but in JSP files only. Rename your .html files to .jsp. This way EL like ${param.id} will then also work, even though you still have a XSS attack hole open.
See also:
Our JSP wiki page
Our Servlets wiki page
(please read them, they contains hello world examples which should turn on some lights in your head)
You can't use the expression language (i.e. ${param.id}) in plain HTML files. It'll only be interpreted in JSPs (files with a .jsp extension).