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>
Related
I am comparing the two string in the IF block and both of the strings are not null but a really weird error is being shown. Even though this same code is working with using simply servlets. But when I run this code in JSP it gives the error. I have already checked if the values were null but the output is exactly what was input in the form. My code is this
<%
String sender = (String) request.getAttribute("sender");
System.out.println(request.getParameter("data") + " " + request.getParameter("sender"));
String process = "process";
if (sender.equals(process)) {
String itemName = request.getParameter("data");
%>
<%="No Item with name: " + itemName + " Found"%>
<%
}
%>
This is the error that is being shown.
Some Error: An exception occurred processing [/error.jsp] at line [31] 28: String sender = (String) request.getAttribute("sender"); 29: System.out.println(request.getParameter("data") + " " + request.getParameter("sender")); 30: String process = "process"; 31: if (sender.equals(process)) { 32: 33: String itemName = request.getParameter("data"); 34: %> Stacktrace: org.apache.jasper.JasperException: An exception occurred processing [/error.jsp] at line [31] 28: String sender = (String) request.getAttribute("sender"); 29: System.out.println(request.getParameter("data") + " " + request.getParameter("sender")); 30: String process = "process"; 31: if (sender.equals(process)) { 32: 33: String itemName = request.getParameter("data"); 34: %> Stacktrace: [Ljava.lang.StackTraceElement;#6fb251a5
Folllowing is my complete page code
<%# page contentType="text/html;charset=UTF-8" language="java" %>
<!doctype html>
<html class="no-js" lang="">
<head>
<meta charset="utf-8">
<title>No Data</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="css/normalize.css">
<link rel="stylesheet" href="css/main.css">
<meta name="theme-color" content="#fafafa">
</head>
<body>
<section id="google-form" class="section google-form">
<div class="account_create-text center ">
<h4>Oops No data found. </h4>
</div>
<br>
<span class="username_curr-email blue ce block spacer-10">!
<%
String sender = (String) request.getAttribute("sender");
System.out.println(request.getParameter("data") + " " + request.getParameter("sender"));
String process = "process";
if (sender.equals(process)) {
String itemName = request.getParameter("data");
%>
<%="No Item with name: " + itemName + " Found"%>
<%
}
%>
</span>
<span class="spacer-10 block"></span>
<span class="spacer-10 block"></span>
<span class="spacer-10 block"></span>
<br>
<br>
<br>
<div class="bb-30 spacer-10">
<a href="Task1.html" style="text-decoration: none;" class="button-elevated ">
Go back
</a>
</div>
<br>
<br>
</section>
</body>
</html>
Found the answer. Use wrong method.
request.getParameter()
should have been used.
I've following string which is HTML -
<html>
<head>
<title>Repository</title>
</head>
<body>
<h2>Subversion</h2>
<ul>
<li>
..
</li>
<li>
branch_A
</li>
<li>
branch_B
</li>
</ul>
</body>
</html>
Out of this I want to get labels of li tag which are branch_A, branch_B
Count of li's can vary. I want to get all of them. Can you please help how I can parse this String and get those values?
NOTE I could have used jsoup library to achieve same, but considering our project restriction, I cannot use it.
You can use an HTML parser for this. In the code below jsoup (https://www.baeldung.com/java-with-jsoup) is used and its quick and easy.
Document doc = Jsoup.connect(fix url here).get();
doc.select(tag you want).forEach(System.out::println);
Other tools are discussed here: https://tomassetti.me/parsing-html/
Using Java 8 streams:
String html = "<html>\n" +
" <head>\n" +
" <title>Repository</title>\n" +
" </head>\n" +
" <body>\n" +
" <h2>Subversion</h2>\n" +
" <ul>\n" +
" <li>\n" +
" ..\n" +
" </li>\n" +
" <li>\n" +
" branch_A\n" +
" </li>\n" +
" <li>\n" +
" branch_B\n" +
" </li>\n" +
" </ul>\n" +
" </body>\n" +
"</html>";
html.lines().filter(line -> line.contains("<a href")).forEach(System.out::println);
Output:
..
branch_A
branch_B
Keep in mind you can run streams in parallel if you have huge HTML file.
Also you can strip HTML tags using map:
html.lines().filter(line -> line.contains("<a href")).map(line -> line.replaceAll("<[^>]*>","")).forEach(System.out::println);
Output:
branch_A
..
branch_B
Here is question scenario
<html>
<body>
<div class="specalClass">
<table>
<tbody id="mainTable">
<tr><td>data 1</td></tr>
<tr><div>Data</div></tr>
</tbody>
</table>
</div>
</body>
</html>
There is a problem, how to get div that is directly placed in tr tag, all element are traceable, except this div.
This is just a sample code: we can not use XPath or div-tag directly, because the real page is a big one. We can get this table by its id and then need to iterate it.
You can use CSS selector to get div which is direct child of tr in table with id mainTable:
doc.select("#mainTable tr>div");
but it won't work because we have another problem here.
div is not allowed in tr so Jsoup's HTML parser removes it because it follows the standard. To skip HTML validation you should parse the document using XML parser:
Document doc = Jsoup.parse(html, "", Parser.xmlParser());
It will keep the original structure and now that div will be reachable.
EDIT:
To counter the accusations of posting not working answer I'm pasting whole code with the output of both HTML and XML parsers.
The first example doesn't work, but the second one according to my answer works fine:
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.parser.Parser;
public class Stackoverflow62376512 {
public static void main(final String[] args) {
String html = "<html>\n" +
" <body>\n" +
" <div class=\"specalClass\">\n" +
" <table>\n" +
" <tbody id=\"mainTable\">\n" +
" <tr><td>data 1</td></tr>\n" +
" <tr><div>Data</div></tr>\n" +
" </tbody>\n" +
" </table>\n" +
" </div>\n" +
" </body>\n" +
"</html>";
Document doc = Jsoup.parse(html, "", Parser.htmlParser()); // same as Jsoup.parse(html);
System.out.println("Document parsed with HTML parser (div inside tr will be dropped): " + doc);
System.out.println("Selecting div (this will fail and show null): " + doc.select("#mainTable tr>div").first());
System.out.println("\n-------------\n");
doc = Jsoup.parse(html, "", Parser.xmlParser());
System.out.println("Document parsed with XML parser (div inside tr will be kept): " + doc);
System.out.println("Selecting div (this one will succeed): " + doc.select("#mainTable tr>div").first());
}
}
and the output is:
Document parsed with HTML parser (div inside tr will be dropped): <html>
<head></head>
<body>
<div class="specalClass">
<div>
Data
</div>
<table>
<tbody id="mainTable">
<tr>
<td>data 1</td>
</tr>
<tr></tr>
</tbody>
</table>
</div>
</body>
</html>
Selecting div (this will fail and show null): null
-------------
Document parsed with XML parser (div inside tr will be kept): <html>
<body>
<div class="specalClass">
<table>
<tbody id="mainTable">
<tr>
<td>data 1</td>
</tr>
<tr>
<div>
Data
</div>
</tr>
</tbody>
</table>
</div>
</body>
</html>
Selecting div (this one will succeed): <div>
Data
</div>
Here, a table of numbers is generated with a loop. Each value in the table is a button, which, when clicked, displays 10 times the value in the pop-up. In the actual project, the data is in jsp, so I cannot use javascript.
Please help.
Here is my JSP:
<HTML>
<title> Hi </title>
<%
out.println(" <table border = \"1\" align=\"center\" width='600'>\n" +
" <tr>\n" +
" <th> Number </th>\n" +
" </tr>");
int i;
for(i=0;i<10;i++){
out.println(" <tr>\n" +
" <td style = \"text-align:center\" height=\"40\"> <font style=\"color: black >" + "<button onclick=\"document.getElementById('id01').style.display='block'\" style=\"width:auto; -webkit-box-align: center; \"> " +i+ " </button> </a>" +"</font> </td>\n" +
" </tr> ");
}
%>
</HTML>
DIV:
<div id="id01" class="modal">
<div class="modal-content animate" >
<div class="imgcontainer">
<span onclick="document.getElementById('id01').style.display='none'" class="close" title="Close Modal">×</span>
<img src="doctor.jpg" alt="Avatar" class="avatar">
</div>
<div class="container"><centre>
<%
//
// I need to display id*10;
//
//
%>
</centre></div>
</div>
</div>
The problem is that i equals 9 by the end of the loop, so 'i' of individual buttons are lost.
Please help.
You could write the whole modal <div> code inside another for loop with changing ids and the onclick for each button will display its corresponding modal popup.
Here is your JSP:
<HTML>
<title> Hi </title>
<%
out.println(" <table border = \"1\" align=\"center\" width='600'>\n" +
" <tr>\n" +
" <th> Number </th>\n" +
" </tr>");
int i;
for(i=0;i<10;i++){
out.println(" <tr>\n" +
" <td style = \"text-align:center\" height=\"40\"> <font style=\"color: black >" + "<button onclick=\"document.getElementById('" + "id" + i + "').style.display='block'\" style=\"width:auto; -webkit-box-align: center; \"> " +i+ " </button> </a>" +"</font> </td>\n" +
" </tr> ");
}
%>
</HTML>
DIV:
<% int j=0;
for (j=0;j<10;j++){ %>
<div id='<%= "id"+j %>' class="modal">
<div class="modal-content animate" >
<div class="imgcontainer">
<span onclick="document.getElementById('<%= "id"+j %>').style.display='none'" class="close" title="Close Modal">×</span>
<img src="doctor.jpg" alt="Avatar" class="avatar">
</div>
<div class="container"><centre>
<%= "id"+j %>
</centre></div>
</div>
</div>
<% } %>
I tested it on TutorialsPoint and seems to work fine.
<%
String song = request.getParameter("songname");
String band = request.getParameter("band");
String url = request.getParameter("url");
%>
<div align="center">
<br><br> <% out.print(band + " - " + song); %><br><br>
<iframe width="560" height="315" src="<%out.print(url);%>" frameborder="0" allowfullscreen></iframe>
</div>
<%
%>
Let's say url i get is like this https://www.youtube.com/watch?v=kXYiU_JCYtU i want to make it look like https://www.youtube.com/embed/kXYiU_JCYtU this so i can put it at src=""
You can use the replace() method:
<% out.print(url.replace("watch?v=", "embed/")); %>