How can I pass value from javascript to java applet? - java

import java.awt.*;
import java.applet.*;
public class MyApplet extends Applet{
/**
*
*/
private static final long serialVersionUID = 1L;
String dString = "Value is null";
String gPath="";
public void get_path(){
gPath = this.getParameter("path");
if(gPath == null)
gPath = dString;
//setBackground(Color.cyan);
}
public void paint(Graphics g){
g.drawString(gPath, 20, 30);
}
}
HTML code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title> Upload File </title>
<script type="text/javascript">
var sys_path;
function print_path(){
sys_path = document.getElementById('name').value;
document.app.get_path();
document.write(sys_path);
}
</script>
<applet code="MyApplet.class" name="app" width="300" height="200">
<param name="path" value="&{sys_path};">
</applet>
</head>
<body>
<form>
Path:<input type="text" id="name" \>
<input type="button" value="click" onClick="print_path();" />
</form>
</body>
</html>
Explanation:
In these code I'm trying to take input from text box and display in the applet. I'm trying this couple of days and unable to find a way. Please help me regarding this. Thanks in advance!

Related

How to dynamically generate SVG image from java code in thymeleaf?

I am stuck at one point where I have to display a SVG image on the springboot webpage, which is generated from Graphviz code in Java.
Any help is highly appreciated.
This is my controller class.
#RequestMapping(value = "/e2emap", method = RequestMethod.GET)
public String getE2E_WebPage(Model model
#RequestParam(name = Query_refresh_param, required = false) String refreshInterval) throws IOException {
final var graph = graphGenerator.createGraph();
final String graphString = java.util.Base64.getEncoder().encodeToString(graph);
model.addAttribute("graphVariable", graphString);
model.addAttribute("graph", graph);
return "Dynamic";
Here is the Dynamic.html file.
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset = "UTF-8" />
<link href="../static/css/styles.css" data-th-href="#{/css/styles.css}" rel="stylesheet" />
<title>SRE End-2-End flow</title>
</head>
<body>
<div class="title"><a class="a-sre-e2e" >SRE End-2-End Flow</a></div>
<meta http-equiv= "refresh" content = "15 " />
<p>
<form action="/e2emap" method="get">
<span class="variable-title">Target Environment</span>
<select class="variable-combobox-values" name="target_env" id="target_env" onchange="submit()">
<option th:each="env : ${environments}" th:text="${env}" th:selected="${env}==${targetEnv}">sample-env</option>
</select>
</form>
<h1 th:text="${targetEnv}">bla-bla</h1>
<span>
<img th:src="#{'data:graph/svg;base64' + ${graph}}"> </img> Image
</span>
</body>
</html>
Some points to specify:
method createGraph() is the main method that uses graphviz to generate the graph and returns the graph in byteArray[] form.
The html has a drop down list to select different values. On-click, it should display the graph, which is not happening at the moment.

(Unnecessary) markup in a Wicket form

While generating a simplistic form page using Wicket (version 7.5.0), I'm getting extra markup which seems unnecessary (a hidden field placed into a <div> with inline CSS):
<!DOCTYPE html>
<html xmlns:wicket="http://wicket.apache.org">
<head>
<meta charset="utf-8" />
<title>Apache Wicket Quickstart</title>
<link href='http://fonts.googleapis.com/css?family=Yanone+Kaffeesatz:regular,bold' rel='stylesheet' type='text/css' />
<link rel="stylesheet" href="mystyle.css" type="text/css" media="screen" title="Stylesheet"/>
</head>
<body>
<form method="post" wicket:id="ItemForm" id="ItemForm1" action="./tf?1-1.IFormSubmitListener-ItemForm">
<div style="width:0px;height:0px;position:absolute;left:-100px;top:-100px;overflow:hidden">
<input type="hidden" name="ItemForm1_hf_0" id="ItemForm1_hf_0" />
</div>
<p>
<label for="name">
<span>Item name:</span>
</label>
<input type="text" name="p::name" wicket:id="name" value="">
</p>
<p>
<label for="price">
<span>Item price:</span>
</label>
<input type="text" name="price" wicket:id="price" value="0">
</p>
<section>
<input type="submit" value="Submit">
</section>
</form>
</body>
</html>
The relevant Java class is:
// Package name and imports omitted
public final class ItemFormPage extends WebPage {
#EJB(name = "ejb/item")
Item it;
public ItemFormPage() {
Form f = new Form("ItemForm") {
#Override
public void onSubmit() {
setResponsePage(new ItemDisplay());
}
};
f.setDefaultModel(new CompoundPropertyModel(it));
f.add(new TextField("name"));
f.add(new TextField("price"));
add(f);
}
}
I'm new to Wicket, as is probably evident from the code. Is there a way to avoid generating the aforementioned seemingly unnecessary markup? In other words, am I missing something or should I report a bug?
This hidden input is used to submit the form with anchor-based components like SubmitLink.
For example, you have a Form and you want to have two ways to submit it (with different 2 buttons):
Form<Void> form = new Form<Void>("form") {
#Override
protected void onSubmit() {
// central form onSubmit
}
};
SubmitLink submitter1 = new SubmitLink("submitter1") {
#Override
public void onSubmit() {
System.out.println("submitter 1 called");
}
};
form.add(submitter1);
SubmitLink submitter2 = new SubmitLink("submitter2") {
#Override
public void onSubmit() {
System.out.println("submitter 2 called");
}
};
form.add(submitter2);
When you click any of the two submitters, its name will be put to that input, and Wicket will find the correct SubmitLink component and call its onSubmit().

How do we print a counter from a servlet to a jsp?

Hey I am trying to create a voting app.
I have several counters in my servlets and I want to be able to display the value of those counters in my jsp file. How do I do it?
Please help me.
I want to display the total number of votes in each section and the winner after the election.
......................................................................................................................................................
servlet file
......................................................................................................................................................
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
#WebServlet("/CastVote")
public class CastVote extends HttpServlet {
int javaCount, phpCount, pythonCount, othersCount;
public CastVote() {
super();
javaCount = 0;
phpCount = 0;
pythonCount = 0;
othersCount = 0;
// TODO Auto-generated constructor stub
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String choice = request.getParameter("language");
if("java".equals(choice))
{
javaCount++;
}
else if("php".equals(choice))
{
phpCount++;
}
else if("python".equals(choice))
{
pythonCount++;
}
else if("others".equals(choice))
{
othersCount++;
}
}
}
......................................................................................................................................................
Jsp File
......................................................................................................................................................
<%# 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>Vote now.</title>
</head>
<body>
<center>
<h2>Welcome to voting portal. Cast your vote.</h2>
</center>
<form action="CastVote" method="post">
Which of the following is the best language for backend programming? <br>
<input type="radio" name="language" value="java">1) Java <br>
<input type="radio" name="language" value="php">2) PHP <br>
<input type="radio" name="language" value="python">3) Python <br>
<input type="radio" name="language" value="others">4) Others <br>
<input type = "submit" value ="Vote">
</form>
</body>
</html>

How to create multi layout in Spring MVC

I know about Apache Tiles in Spring, it seem working same jsp:include, but it doesn't solve my problem:
I want a file with name is layout1.jsp, in this file, I will define a layout like:
<html>
<head>
<style href="style1.css" />
</head>
<body>
<div class="main">
<div class="left">
<ul>
<li>
<li>
<li>
</ul>
</div>
<div class="content">
<h1>${message }</h1>
</div>
<div class="footer">
<span>This is footer</span>
</div>
</div>
</body>
</html>
And a file is layout2.jsp:
<html>
<head>
<style href="style2.css" />
</head>
<body>
<div class="main">
<div class="left2">
<ul>
<li>
<li>
<li>
</ul>
</div>
<div class="content2">
<h1>${message }</h1>
</div>
<div class="footer2">
<span>This is footer</span>
</div>
</div>
</body>
</html>
When user chose layout name on combobox, controller will set layout dynamic before render layout.
How should I do?
For switching the css file an you could use the Spring Theme Resolver. And you can use the same mechanism to change some small parts of your html (like the div classes)
<%# taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<html>
<head>
<style href="<spring:theme code='styleSheet'/>" />
</head>
<body>
...
<div class="<spring:theme code='contentClass'/>">
<h1>${message }</h1>
</div>
...
</body>
</html>
(But I would recommend to use the same html with same classes and just switch the css).
Do not forget to setup the theme resolver!
#See Spring Reference: Chapter Using themes
I found a method to solve this problem:
My application separator to multi module, example "Admin", "Default", each module have a interceptor, example a module interceptor:
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
String servletPath = request.getServletPath();
if (!servletPath.equals("/") && servletPath.substring(servletPath.length() - 1).equals("/")) {
response.sendRedirect(request.getContextPath() + servletPath.replaceAll("\\/+$", ""));
return false;
}
return true;
}
In view I have a class InternalResourceViewResolver.java:
package view;
import helper.AppHelper;
import java.util.Locale;
import org.springframework.web.servlet.View;
public class InternalResourceViewResolver extends
org.springframework.web.servlet.view.InternalResourceViewResolver {
public View resolveViewName(String viewName, Locale locale)
throws Exception {
AppHelper.setPage("../" + AppHelper.getModule() + "/" + viewName + ".jsp");
return super.resolveViewName("layout/" + AppHelper.getLayout(), locale);
}
}
AppHelper class:
package helper;
public class AppHelper {
private static String module = "default";
private static String layout = "main";
private static String page = "index";
public static String getModule() {
return module;
}
public static void setModule(String module) {
AppHelper.module = module;
}
public static String getLayout() {
return layout;
}
public static void setLayout(String layout) {
AppHelper.layout = layout;
}
public static String getPage() {
return page;
}
public static void setPage(String page) {
AppHelper.page = page;
}
}
In other module, example for Admin:
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
System.out.println("ServletPath" + request.getServletPath());
AppHelper.setModule("admin");
AppHelper.setLayout("admin");
return true;
}
And my view page:
In layout:
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!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 href="<c:url value="/resources/css/main.css" />" rel="stylesheet">
<title>Welcome to web</title>
</head>
<body>
<div class="wrapper">
<div class="main">
<div class="header"></div>
<div class="content">
<div class="content-left">
<jsp:include page="<%=helper.AppHelper.getPage() %>"></jsp:include>
</div>
<div class="content-right"></div>
</div>
<div class="footer"></div>
</div>
</div>
</body>
</html>
Placeholder page:
<%#taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<h1>${message }</h1>
<h2>${welcome }</h2>
<c:forEach items="${batches }" var="batch">
${batch.id } - ${batch.name }
</c:forEach>
Here directory struct:
- src\main\webapp\WEB-INF\pages
- admin
- index
- index.jsp
- setting.jsp
- product
- index.jsp
- detail.jsp
- other
...
- default
- index
- index.jsp
- contact.jsp
- product
...
- layout
admin.jsp
main.jsp
You could do this using Tiles too ...
use apache-tiles 2.2.1 or above
In application's tiles.xml where you put layout models do this
<definition name="masterTile" templateExpression="${requestScope.layout} >
<put-attribute name="title" value="" />
<put-attribute name="header" value="/view/header.jsp" />
<put-attribute name="menu" value="/view/menu"/>
<put-attribute name="body" value="" />
<put-attribute name="footer" value="/view/footer.jsp"/>
</definition>
For more on this refer here Tiles Expression Language Support

POST an array of custom objects to a Struts 2 action

How do I POST an array of custom objects to a Struts 2 action in Java?
For example if I have the following Java object:
public class Person {
private String name;
private String lastName;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
And the following Action:
public class SavePersons extends ActionSupport {
private List<Person> persons;
#Override
public String execute() throws Exception {
// Do something
return SUCCESS;
}
public void setPersons(List<Person> persons) {
this.persons = persons;
}
}
I'd like to do something like this in an HTML form:
<html>
<body>
<form method="POST" action="http://postHere">
<input type="text" name="persons[0].name" value="Name1"/>
<input type="text" name="persons[0].lastName" value="LastName1"/>
<input type="text" name="persons[1].name" value="Name2"/>
<input type="text" name="persons[1].lastName" value="LastName2"/>
<input type="submit" />
</form>
</body>
</html>
Any tips?
What you have looks good. It does not make a difference to struts2 if you post or get as far as setting values.
Using the same SavePersons class, except that I added a public List<Person> getPersons() method. This is required to make the solution work.
And using essentially the same form, except I prefer to write my forms using s2 tags where it makes sense (what puts some people off the form tags is the default s2 theme, you can globally set the theme to simple, the label attribute will not work but the UI tags will work just like you'd expect similar html elements to behave):
<%#taglib prefix="s" uri="/struts-tags"%>
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Person Form</title>
</head>
<body>
<h1>Person Form</h1>
<s:form action="person-test" method="post">
<s:textfield name="persons[0].name" label="fName 1"/>
<s:textfield name="persons[0].lastName" label="lName 1"/>
<s:textfield name="persons[1].name" label="fName 2"/>
<s:textfield name="persons[1].lastName" label="lName 2"/>
<s:submit/>
</s:form>
</body>
</html>
Note that method="post" is not needed, it is the default.
Here is the page used to display the form data.
<%#taglib prefix="s" uri="/struts-tags"%>
<%#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>List of People</h1>
<s:iterator value="persons">
<s:property value="name"/> <s:property value="lastName"/><br/>
</s:iterator>
</body>
</html>
And it worked just fine.

Categories

Resources