I'm starting to learn servlet and I'm using MySQL and Tomcat. I'm trying to make a simple servlet that when the submit button is clicked it will display a different set of instruction by calling the doGet() function again and moves the cursor to the next set.
protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
{
out.println("<HTML><HEAD><TITLE>Instructions</TITLE><HEAD>");
out.println("<BODY>");
ResultSet rs = stmt.executeQuery("SELECT InstructNo, Instruct FROM Intructions");
if(rs.next())
{
out.println(rs.getString("InstructNo") + ". " + rs.getString("Instruct"));
}
out.println("<form method=\"get\" action=\"\">");
out.println("<input type=\"radio\" name=\"ans\" value=\"true\">True<br>");
out.println("<input type=\"radio\" name=\"ans\" value=\"true\">False<br>");
out.println("<input type=\"submit\" value=\"Submit\">");
out.println("</BODY></HTML>");
I can't figure out how to move the cursor to the next set when submit button is clicked and
call the doGet() function gets called again.
First you need store something that lets you know where you are in the list. So, for example, add a hidden form field with some sort of value that lets you read the right thing from your database. I'll assume your records in the db are numbered.
out.println("<input type=\"radio\" name=\"ans\" value=\"true\">False<br>");
out.println("<input type='hidden' name='index' value='" + index + "' />");
out.println("<input type=\"submit\" value=\"Submit\">");
Then you need to collect the that value when the server gets the request and use it to read the next value:
String index = req.getParameter("index");
int idx = Integer.parseInt(index); // add code to detect an exception here
ResultSet rs = stmt.executeQuery("SELECT InstructNo, Instruct FROM Intructions where Index=" + index); // Check my rusty SQL here
Then figure out the next one:
index = "" + (idx+1); // The +1 means to get the next one next time
out.println("<form method=\"get\" action=\"\">");
What about replacing the if statement with a while loop for iterating over all the records in the database and then you can use the same code to print it to the web page.
You may also want to learn about using JSPs which makes the separation between the controller (the servlet) and the view (the web page presented to the user; or the JSP).
Related
Sorry for the longer post, I'm trying to be specific. I'm a bit of a newb at cold fusion and lucee, so forgive me if I have missed something fundamental here. I'm just trying to do a quick POC, but can't get it working. What I am trying to do is make a page call, write to a web page, sleep for a while. Kind of a heartbeat. What I can't get to happen is the write to the web page...until all sleep(s) have happened and the page cfm file completes processing. I've looked extensively for the past couple of days, and have tried numerous items, but to no avail. From my index.cfm lucee page, I have a link to launch a new tab and call my cfm file.
<a href="./pinger2.cfm" target="_blank"><img class="ploverride" src="/assets/img/Ping.png" alt="Ping Test" width="125" height="75">
No problem here, a new tab opens and pinger2.cfm starts processing.
What I'm hoping for is the table heading to almost immediately print to the page, then make the first call out, print the results to the page, sleep, make the next call out, print to the page...but it no workey. Anyone have a clue? The code in the pinger2.cfm file is:
<cfscript>
public struct function pinger( required string url, required string verb, required numeric timeout, struct body )
{
var result = {
success = false,
errorMessage = ""
};
var httpService = new http();
httpService.setMethod( arguments.verb );
httpService.setUrl( arguments.url );
httpService.setTimeout( arguments.timeout );
if( arguments.verb == "post" || arguments.verb == "put" )
{
httpService.addParam(type="body", value=SerializeJSON(arguments.body));
}
try {
callStart = getTickCount();
var resultObject = httpService.send().getPrefix();
callEnd = getTickCount();
callLength = (callEnd-callStart)/1000;
if( isDefined("resultObject.status_code") && resultObject.status_code == 200 )
{
result.success = true;
logMessage = "Pinger took " & toString( callLength ) & " seconds.";
outLine = "<tr><td>" & resultObject.charset & "</td><td>" & resultObject.http_version & "</td><td>" & resultObject.mimetype & "</td><td>" & resultObject.status_code & "</td><td>" & resultObject.status_text & "</td><td>" & resultObject.statuscode & "</td><td>" & logMessage & "</td></tr>";
writeOutput( outLine );
getPageContext().getOut().flush();
return result;
}
else
{
throw("Status Code returned " & resultObject.status_code);
}
}
catch(Any e) {
// something went wrong with the request
writeDump( e );
abort;
}
}
outLine = "<table><tr><th>charset</th> <th>http_version</th> <th>mimetype</th> <th>status_code</th> <th>status_text</th> <th>statuscode</th> <th>time</th> </tr>";
writeOutput( outLine );
getPageContext().getOut().flush();
intCounter = 0;
while(intCounter LT 2)
{
theResponse = pinger(
url = "https://www.google.com",
verb = "GET",
timeout = 5
);
intCounter = intCounter + 1;
getPageContext().getOut().flush();
sleep(2000);
}
outLine = "</table>";
writeOutput( outLine );
</cfscript>
NOTE: I'm sure there are other "less than best" practices in there, but I'm just trying to do this quick and dirty.
I thought the getPageContext().getOut().flush(); would do the trick, but no bueno.
EDIT: If it matters, I'm using CF version 10,0,0,0 and Lucee version 4.5.2.018.
I do something similar to generate ETags by hand (using Lucee 4.5). I stick a simple
GetPageContext().getOut().getString()
in the onRequestEnd function in Application.cfc. This returns a string of HTML just like it's sent to the browser.
You could store that in the appropriate scope (APPLICATION, SESSION, etc) and use it later, or whatever you need. Obviously, all processing needs to be completed, but it shouldn't require any flushes. In fact, flushing may or may not alter its behavior.
I am running an azure queue function on a consumption plan; my function starts an FFMpeg process and accordingly is very CPU intensive. When I run the function with less than 100 items in the queue at once it works perfectly, azure scales up and gives me plenty of servers and all of the tasks complete very quickly. My problem is once I start doing more than 300 or 400 items at once, it starts fine but after a while the CPU slowly goes from 80% utilisation to only around 10% utilisation - my functions cant finish in time with only 10% CPU. This can be seen in the image shown below.
Does anyone know why the CPU useage is going lower the more instances my function creates? Thanks in advance Cuan
edit: the function is set to only run one at a time per instance, but the problem exists when set to 2 or 3 concurrent processes per instance in the host.json
edit: the CPU drops get noticeable at 15-20 servers, and start causing failures at around 60. After that the CPU bottoms out at an average of 8-10% with individuals reaching 0-3%, and the server count seems to increase without limit (which would be more helpful if I got some CPU with the servers)
Thanks again, Cuan.
I've also added the function code to the bottom of this post in case it helps.
using System.Net;
using System;
using System.Diagnostics;
using System.ComponentModel;
public static void Run(string myQueueItem, TraceWriter log)
{
log.Info($"C# Queue trigger function processed a request: {myQueueItem}");
//Basic Parameters
string ffmpegFile = #"D:\home\site\wwwroot\CommonResources\ffmpeg.exe";
string outputpath = #"D:\home\site\wwwroot\queue-ffmpeg-test\output\";
string reloutputpath = "output/";
string relinputpath = "input/";
string outputfile = "video2.mp4";
string dir = #"D:\home\site\wwwroot\queue-ffmpeg-test\";
//Special Parameters
string videoFile = "1 minute basic.mp4";
string sub = "1 minute sub.ass";
//guid tmp files
// Guid g1=Guid.NewGuid();
// Guid g2=Guid.NewGuid();
// string f1 = g1 + ".mp4";
// string f2 = g2 + ".ass";
string f1 = videoFile;
string f2 = sub;
//guid output - we will now do this at the caller level
string g3 = myQueueItem;
string outputGuid = g3+".mp4";
//get input files
//argument
string tmp = subArg(f1, f2, outputGuid );
//String.Format("-i \"" + #"input/tmp.mp4" + "\" -vf \"ass = '" + sub + "'\" \"" + reloutputpath +outputfile + "\" -y");
log.Info("ffmpeg argument is: "+tmp);
//startprocess parameters
Process process = new Process();
process.StartInfo.FileName = ffmpegFile;
process.StartInfo.Arguments = tmp;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.WorkingDirectory = dir;
//output handler
process.OutputDataReceived += new DataReceivedEventHandler(
(s, e) =>
{
log.Info("O: "+e.Data);
}
);
process.ErrorDataReceived += new DataReceivedEventHandler(
(s, e) =>
{
log.Info("E: "+e.Data);
}
);
//start process
process.Start();
log.Info("process started");
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
}
public static void getFile(string link, string fileName, string dir, string relInputPath){
using (var client = new WebClient()){
client.DownloadFile(link, dir + relInputPath+ fileName);
}
}
public static string subArg(string input1, string input2, string output1){
return String.Format("-i \"" + #"input/" +input1+ "\" -vf \"ass = '" + #"input/"+input2 + "'\" \"" + #"output/" +output1 + "\" -y");
}
When you use the D:\home directory you are writing to the virtual function, which means each instance has to continually try to write to the same spot as the functions run which causes the massive I/O block. Instead writing to D:\local and then sending the finished file somewhere else solves that issue, this way rather than each instance constantly writing to a location they only write when completed, and write to a location designed to handle high throughput.
The easiest way I could find to manage the input and output after writing to D:\local was just to hook up the function to an azure storage container and handle the ins and outs that way. Doing so made the average CPU stay at 90-100% for upwards of 70 concurrent Instances.
this is my first post here, I'm glade to hear from you :-)
Here's my question, please.
In my JqueryMobile web application I've created categories and records.
Each category hosts different records.
What I'd like to print out to the user is the number of records that exists in each category.
For selecting the categories I made a normal transaction
function getAll() {
db.transaction(function (transaction) {
transaction.executeSql(("SELECT * FROM tags"), [], getAllSuccess, errorCB);
});
}
function getAllSuccess(tx, result) {
$('#bags_ul').empty();
var output = '';
$.each(result.rows, function (index) {
var row = result.rows.item(index);
output += '<li><a href="#" class="link_to_tag" data-tagnome="' + row['tagnome'] + '" data-tagtipo="' + row['tagtipo'] + '" data-tagid="' + row['tagid'] + '" >' + row['tagnome'] + '</a></li>';
});
$('#bags_ul').html(output);
$('#bags_ul').listview();
$('#bags_ul').listview('refresh');
}
In my example, the function getAll makes the query, and if get success it execute the getAllSuccess function, that fills in the LI fields of my list.
The problem is that I need to create a query inside the $.each() to get the records of the category.
I've tried to execute another transaction within it, and I can successfully get the number of records thorugh the console.log function, but the problem is that I don't know how to pass it to the "mother" function that in facts pushes the HTML to the page.
Could you please help me?
As you have understood, I need to know the "logic" that is behind this.
I really do thank you very much for your help.
Marco
You could chain the callbacks yourself or use JQuery Deferred.
today's question involves URLLoader requests using encrypted strings.
when I encrypt a string I get the following result:
1Kx4dfp5OC7ox0zb0lWzzzlnoPLcoPGE1MrAKOtl3h6SPcFmEdpLnUROSKpPrCl70VHRxrKzhsxHHlb1MRp3++JkvYZ++ghBEG2zbVhyaqQ/0+NDrJ+0cLt3g9THe9POohN6Ufcq9TcnmZVvIFXllg4HrjVNfQrhQCNwxuBgWBf2DRc4eq6hKzEgyLdlllQFc9ssUFlPD3wOBqoI22r+7N82sI3pqsQYBq5VlKHHreqD8Cq0gictnTFS3IqepASGARKyuCIPDCa4zE76VeQV5zgvkFfjDww+C1uZ8PUgjH67DKYqUP9a6euf2v1jUpBrREnm4ZbLAXScDjvrJ11rWYyVXOLZy9nhy9qRBQRvdw+tnBThPTmvxaq+LAusF8IbvDpZgMrZ3buvThnXuSBGXZxaja7fk/FIlm4RSliDTSGySiizFHy7dJePXuV0c9MI6ciOYxmEIg64NnhBZtB8wipUDJWOpoytOD2/sNQBenjZbYN8291msYnbBG+alAOQmEBH5Mn4KyW1VQWE2lBGk9ML+SflND8UXfdHz5Q3psOcMZJxSAURKGq5tjA8KlPPOAdQuVPIcysg2/4lV25QGIdDttQVGrkP+ZHZcHIPTLLD+Vml+PJU/OAJGNPGlf3wawUo+bID0FKur8N6tNyu7Pnoocn7plDi6WSJgUAaYjI4=
I send it in, everything seems fine on Flex's end. But when I go to the serverside (logfiles, not allowed to change server-side code) to check what I'm getting, I end up with this:
1Kx4dfp5OC7ox0zb0lWzzzlnoPLcoPGE1MrAKOtl3h6SPcFmEdpLnUROSKpPrCl70VHRxrKzhsxHHlb1MRp3 JkvYZ ghBEG2zbVhyaqQ/0 NDrJ 0cLt3g9THe9POohN6Ufcq9TcnmZVvIFXllg4HrjVNfQrhQCNwxuBgWBf2DRc4eq6hKzEgyLdlllQFc9ssUFlPD3wOBqoI22r 7N82sI3pqsQYBq5VlKHHreqD8Cq0gictnTFS3IqepASGARKyuCIPDCa4zE76VeQV5zgvkFfjDww C1uZ8PUgjH67DKYqUP9a6euf2v1jUpBrREnm4ZbLAXScDjvrJ11rWYyVXOLZy9nhy9qRBQRvdw tnBThPTmvxaq LAusF8IbvDpZgMrZ3buvThnXuSBGXZxaja7fk/FIlm4RSliDTSGySiizFHy7dJePXuV0c9MI6ciOYxmEIg64NnhBZtB8wipUDJWOpoytOD2/sNQBenjZbYN8291msYnbBG alAOQmEBH5Mn4KyW1VQWE2lBGk9ML SflND8UXfdHz5Q3psOcMZJxSAURKGq5tjA8KlPPOAdQuVPIcysg2/4lV25QGIdDttQVGrkP ZHZcHIPTLLD Vml PJU/OAJGNPGlf3wawUo bID0FKur8N6tNyu7Pnoocn7plDi6WSJgUAaYjI4=
at first glance they're the same, but if you check closely, the + gets replaced by a whitespace...
I've even tried switching the + for %2B but on the server-side it gets read as %2B, it isn't converted to a + (flex doesn't seem to function as a browser in this case).
Any kind of insight and help on this matter would be very appreciated.
The requests are being done as follows:
public function callService(callback:String, request:String):void{
var url:URLRequest = new URLRequest(server);
var requestedString:String = handlePluses(request);
url.useCache = false;
url.contentType = contentType;
url.method = method;
trace("sending: " + requestedString);
url.data += requestedString);
serverURL.addEventListener(IOErrorEvent.IO_ERROR, treatIO);
serverURL.dataFormat = URLLoaderDataFormat.TEXT;
serverURL.addEventListener(Event.COMPLETE, loadData);
serverURL.addEventListener(Event.CONNECT, function():void{trace("connected");});
try{
serverURL.load(url);
}catch(e:ArgumentError){trace("ArgError: " + e.message);}
catch(e:SecurityError){trace("SecError: " + e.message);}
catch(e:TimeoutEvent){trace("===========<Timeout>===========");}
}
we fixed this problem by switching the + character with a subset of escaped characters like \&\#.
this might be a problem to others attempting the same thing and trying to keep to a minimum size.
I've writen a servlet that builds an html page showing the content of a database. The code is:
Statement st = (Statement) conexion.createStatement();
ResultSet rs = st.executeQuery("select * from audiolist" );
while (rs.next())
{
contador++;
out.println("<tr>");
String k = rs.getString("Tittle");
String l = rs.getString("Autor");
String m = rs.getString("Album");
out.println("<td>"+"<input type=\"radio\" name=\"titulo<%="+contador+"%>\"value=\""+k+"\">");
out.println("<td>" + k + "</td>");
out.println("<td>" + l + "</td>");
out.println("<td>" + m + "</td>");
out.println("</tr>");
}
out.println("</table></center>");
out.println("<tr><td colspan=2><input type=submit></td></tr>");
out.println("</form>");
I've added a radio button to each row. With this code I get to show in the browser a table with the content of the database. When I click on submit I want send to another servlet the vale 'k' for the row selected. I'm having a hard time with this. I think I'm sending the value incorrectly. In the second servlet, is it enough to use getParameter() in order to get the info?
Thanks!
In the second servlet you can use:
String value = request.getParameter("tituloX");
to read the value. You need to know the name of the parameter to do. If this is not known, you can try to enumerate the parameters:
for ( Enumeration e = request.getParameterNames(); e.hasMoreElements();) {
String param = (String) e.nextElement();
String value = request.getParameter(param );
}
This only works for parameters with a single value.
Is this line correct?
out.println("<td>"+"<input type=\"radio\" name=\"titulo<%="+contador+"%>\"value=\""+k+"\">");