I have an HTML form in a servlet with several input parms, when I press the Submit button, I want to take the values from the form and write those to a file. I am not certain how to capture the parms to be used in the next .jsp. Here is my code:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import java.text.*;
import java.util.Random;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;
public class CreateCust extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
// Generate a Customer Account using the Random Number Generator
// Calculate Confirmation Number
Random randomGenerator = new Random();
int custNum = randomGenerator.nextInt(10000);
String custId = "CST" + custNum;
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Customer Profile Information";
String MainPageURL =
response.encodeURL("/csj/CustomerAccounts.html");
String docType =
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
"Transitional//EN\">\n";
out.println(docType +
"<HTML>\n" +
"<HEAD><TITLE>" + title + "</TITLE></HEAD>\n" +
"<BODY BGCOLOR=\"#FDF5E6\">\n" +
"<H1 ALIGN=\"CENTER\">" + title + "</H1>");
out.println("<BR>" +
"<BR>" +
"Previous Page");
out.println
("<Form>\n" +
"<CENTER>" +
"Please Enter the following Information:\n" +
"<TABLE border=1>" +
"<TR>" +
"<TD>" +
"Customer ID: " +
"</TD>" +
"<TD>" +
"<Label>" + custId + " </Label>\n" +
"</TD>" +
"</TR>" +
"<TR>" +
"<TD>" +
"Name:" +
"</TD>" +
"<TD>" +
"<Input type=\"Text\" name=\"F_name\" </input>\n" +
"</TR>" +
"<BR>" +
"<TR>" +
"<TD>" +
"Street Address:" +
"</TD>" +
"<TD>" +
"<Input type=\"Text\" name=\"F_Add1\" </input>\n" +
"</TD>" +
"</TR>" +
"<BR>" +
"<TR>" +
"<TD>" +
"Apt or Suite:" +
"</TD>" +
"<TD>" +
"<Input type=\"Text\" name=\"F_Add2\" </input>\n" +
"</TD>" +
"</TR>" +
"<BR>" +
"<TR>" +
"<TD>" +
"City:" +
"</TD>" +
"<TD>" +
"<Input type=\"Text\" name=\"F_City\" </input>\n" +
"</TD>" +
"</TR>" +
"<BR>" +
"<TR><TD>" +
"State:" +
"</TD><TD>" +
"<Input type=\"Text\" name=\"F_state\" </input>\n" +
"</TD>" +
"</TR>" +
"<BR>" +
"<TR><TD>" +
"Zip Code:" +
"</TD>" +
"<TD>" +
"<Input type=\"Text\" name=\"F_Zip\" </input>\n" +
"</TD>" +
"</TR>" +
"<BR>" +
"<TR>" +
"<TD>" +
"Phone Number:" +
"</TD>" +
"<TD>" +
"<Input type=\"Text\" name=\"F_Phone\" </input>\n" +
"</TD>" +
"</TR>" +
"<BR>" +
"<TR><TD>" +
"Cable Plan:" +
"</TD>" +
"<TD>" +
"<select>\n" +
"<option value=\"selcab\">Select Cable</option>\n" +
"<option value=\"Basic50\">Basic50</option>\n" +
"<option value=\"Basic100\">Basic100</option>\n" +
"<option value=\"Ultimate200\">Ultimate200</option>\n" +
"</select>\n" +
"</TD>" +
"</TR>" +
"<BR>" +
"<TR><TD>" +
"Internet Plan:" +
"</TD>" +
"<TD>" +
"<select>\n" +
"<option value=\"selInt\">Select Internet</option>\n" +
"<option value=\"speedlane\">SpeedLane</option>\n" +
"<option value=\"lightlane\">LightLane</option>\n" +
"</select>\n" +
"</TD>" +
"</TR>" +
"<TABLE>" +
"</CENTER>" +
"</Form>" );
String confirmURL =
response.encodeURL("/csj/ConfirmCust");
// "Proceed to Checkout" button below table
out.println
("</TABLE>\n" +
"<FORM ACTION=\"" + confirmURL + "\">\n" +
"<BIG><CENTER>\n" +
"<INPUT TYPE=\"SUBMIT\"\n" +
" VALUE=\"Create Account\">\n" +
"</CENTER></BIG>" +
"</FORM>");
out.println("</BODY></HTML>");
}
}
when I press the Submit button, I want to take the values from the form
For this you have to do request.getParameter("input parameter name");
Well in your servlet you have 2 forms one having action and another does not have.
See this
out.println
("</TABLE>\n" +
"<FORM ACTION=\"" + confirmURL + "\">\n" +
"<BIG><CENTER>\n" +
"<INPUT TYPE=\"SUBMIT\"\n" +
" VALUE=\"Create Account\">\n" +
"</CENTER></BIG>" +
"</FORM>");
here form action is present but when you press submit it calls confirmURL but mo values are passed(as it is not having any input parameter).
Now in this
out.println
("<Form>\n" +
"<CENTER>" +
"Please Enter the following Information:\n" +
"<TABLE border=1>" +
"<TR>" +
"<TD>" +
"Customer ID: " +
"</TD>" +
"<TD>" +
"<Label>" + custId + " </Label>\n" +
....................
input parameter are present but no submit button and no form action
So no action will be performed and hence no values will be passed.
Related
I'm developing an app with Tradingview widget on Android 6.0 (Marshmallow)
I am running the tradingview code in WebView but it is not showing the chart.
This is my TradingView Widget code;
WebView webView = findViewById(R.id.webviewer);
String symbol = getIntent().getStringExtra("symbol");
String interval = getIntent().getStringExtra("interval");
String data = "<!-- TradingView Widget BEGIN -->\n" +
"<div class=\"tradingview-widget-container\">\n" +
" <div id=\"tradingview_357eb\"></div>\n" +
" <div class=\"tradingview-widget-copyright\"><span class=\"blue-text\">"+symbol+" Chart</span></div>\n" +
" <script type=\"text/javascript\" src=\"https://s3.tradingview.com/tv.js\"></script>\n" +
" <script type=\"text/javascript\">\n" +
" new TradingView.widget(\n" +
" {\n" +
" \"width\": 300,\n" +
" \"height\": 200,\n" +
" \"symbol\": \"BINANCE:"+symbol+"\",\n" +
" \"interval\": \""+interval+"\",\n" +
" \"timezone\": \"Europe/Istanbul\",\n" +
" \"theme\": \"light\",\n" +
" \"style\": \"1\",\n" +
" \"locale\": \"en\",\n" +
" \"toolbar_bg\": \"#f1f3f6\",\n" +
" \"enable_publishing\": false,\n" +
" \"hide_side_toolbar\": false,\n" +
" \"save_image\": false,\n" +
" \"studies\": [\n" +
" \"BB#tv-basicstudies\",\n" +
" \"RSI#tv-basicstudies\",\n" +
" \"StochasticRSI#tv-basicstudies\"\n" +
" ],\n" +
" \"container_id\": \"tradingview_357eb\"\n" +
"}\n" +
" );\n" +
" </script>\n" +
"</div>\n" +
"<!-- TradingView Widget END -->";
webView.getSettings().setJavaScriptEnabled(true);
webView.loadData(data, "text/html", "utf-8");
I tried it on Android 8.0 and it works but not on 6.0. Why? Am I missing something?
I created an app that uses Leaflet in it.
In the screen where the map is, there is also a progressBar where I allow uses to search in a specific radius.
The moment they change the value in the progressBar, it changes the size of the circle marker inside the map.
I had like that my map will change its zoom to fit that circle in it.
My code for loading the map is:
String Map_HTML = "<html>\n" +
"<head>\n" +
"\n" +
" <title>Quick Start - Leaflet</title>\n" +
"\n" +
" <meta charset=\"utf-8\" />\n" +
" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n" +
"\n" +
" <link rel=\"shortcut icon\" type=\"image/x-icon\" href=\"docs/images/favicon.ico\" />\n" +
"\n" +
" <link rel=\"stylesheet\" href=\"https://unpkg.com/leaflet#1.4.0/dist/leaflet.css\" integrity=\"sha512-puBpdR0798OZvTTbP4A8Ix/l+A4dHDD0DGqYW6RQ+9jxkRFclaxxQb/SJAWZfWAkuyeQUytO7+7N4QKrDh+drA==\" crossorigin=\"\"/>\n" +
" <script src=\"https://unpkg.com/leaflet#1.4.0/dist/leaflet.js\" integrity=\"sha512-QVftwZFqvtRNi0ZyCtsznlKSWOStnDORoefr1enyq5mVL4tmKB3S/EnC3rRJcxCPavG10IcrVGSmPh6Qw5lwrg==\" crossorigin=\"\"></script>\n" +
"\n" +
"\n" +
"\n" +
"<style>\n" +
"body {\n" +
"padding: 0;\n" +
"margin: 0;\n" +
"}\n" +
"html, body, #map {\n" +
"height: 100%;\n" +
"width: 100%;\n" +
"}\n" +
"</style>\n" +
"</head>\n" +
"<body>\n" +
"\n" +
"\n" +
"\n" +
"<div id=\"mapid\" style=\"width: " + dpWidth + "px; height: " + dpHeight * 0.3 + "px;\"></div>\n" +
"<script>\n" +
"\n" +
"var mymap = L.map('mapid',{zoomControl: false}).setView([" + Lat + ", " + Lon + "], 10);\n" +
"\n" +
" L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {\n" +
" minZoom: 7,\n" +
" maxZoom: 17,\n" +
" attribution: '© OpenStreetMap contributors' \n" +
" }).addTo(mymap);\n" +
"\n" +
"mymap.attributionControl.setPosition('topleft')\n" +
"L.control.zoom({\n" +
"position: 'bottomright'\n" +
"}).addTo(mymap);\n" +
"L.marker([" + Lat + ", " + Lon + "]).addTo(mymap)\n" +
" .bindPopup(\"<b>My Location</b>\").openPopup();\n" +
"\n" +
"L.circle([" + Lat + ", " + Lon + "], " + Radius + ", {\n" +
" color: 'red',\n" +
" fillColor: '#8275FE',\n" +
" fillOpacity: 0.4,\n" +
" weight: '0'\n" +
"}).addTo(mymap);\n" +
"\n" +
"\n" +
"var popup = L.popup();\n" +
"\n" +
"</script>\n" +
"\n" +
"</body>\n" +
"</html>";
where Radius is a parameter I insert based on the value of the progressBar.
Right now it always initializes the map with zoom: 10 because I don't know how to change it dynamically as I want.
Any way to do so?
Thank you
the map can fit to the bounds of the circle with mymap.fitBounds(circle.getBounds());.
Change your code to:
"var circle = L.circle([" + Lat + ", " + Lon + "], " + Radius + ", {\n" +
" color: 'red',\n" +
" fillColor: '#8275FE',\n" +
" fillOpacity: 0.4,\n" +
" weight: '0'\n" +
"}).addTo(mymap);\n" +
"mymap.fitBounds(circle.getBounds());\n" +
List<Employee> records = employeeServicable.printAll();
int size = records.size();
for (int i = 0; i < size; i++) {
htmlRespone.append("<tr> " +
"<td>" + records.get(i).getId() + "</td>" +
"<td>" + records.get(i).getName() + "</td>" +
"<td>" + records.get(i).getDepartment() + "</td>"+
"<td>" + records.get(i).getPhoneNumber() + "</td>" +
"<td>" + records.get(i).getHireDate() + "</td>" +
"<td>" + records.get(i).getJob() + "</td>" +
"<td>" + records.get(i).getEducationLevel() + "</td>" +
"<td>" + records.get(i).getGender() + "</td>" +
"<td>" + records.get(i).getDateOfBirth() + "</td>" +
"<td>" + Integer.toString(records.get(i).getSalary()) + "</td>"
+ "</tr>");
}
htmlRespone.append("</table");
How can I send a specific row data to a servlet? I will attach a button at the end of each column but how will that button do it?
I am creating a table that I want to have collapsing detail on each row. I am able to get this working for the first row of the table, but for every row after this, the button does not toggle. My code is below. I know I am probably missing something very simple here, but I am fairly new to web development. Any help is appreciated.
.js
for (var z=0;z<documentData.length;z++) {
$("#documents-result-table > tbody").append(
"<tr>" +
"<td>" + documentData[z].documentIdocNumber + "</td>" +
"<td>" + documentData[z].docNumber + "</td>" +
"<td>" + documentData[z].documentType + "</td>" +
"<td>" + documentData[z].status + "</td>" +
"<td>" + documentData[z].createdDate + "</td>" +
"<td>" + documentData[z].modifiedDate + "</td>" +
"<td>" + documentData[z].customerId + "</td>" +
"<td>" + documentData[z].customerName + "</td>" +
"<td><a target=\"_blank\" href=\"" + contextPath + "/viewPdf.do?documentIdocNumber=" + documentData[z].documentIdocNumber + "\" class=\"glyphicon glyphicon-eye-open\"></a></td>" +
"<td><a target=\"_blank\" href=\"" + contextPath + "/saveBak.do?documentIdocNumber=" + documentData[z].documentIdocNumber + "\" class=\"glyphicon glyphicon-floppy-save\"></a></td></tr>"
+ "<tr class=\"collapse\" id=\"collapseme\">" +
"<td></td><td colspan=\"9\"><table class=\"table table-striped table-curved\"><tr><th>Customer Id</th><th>Customer Name</th><th>Billing Plant</th><th>Country Code</th><th>Routing Code</th><th>Language</th></tr>" +
"<tr>" +
"<td>" + documentData[z].customerId + "</td>" +
"<td>" + documentData[z].customerName + "</td>" +
"<td>" + documentData[z].billingPlant + "</td>" +
"<td>" + documentData[z].countryCode + "</td>" +
"<td>" + documentData[z].routingCode + "</td>" +
"<td>" + documentData[z].language + "</td></tr>" +
"</table></td></tr>");
$('.collapse').collapse({toggle: false});
$('button span').click(function() {
$("#collapseme").collapse("toggle");
var span = $('button span').hasClass('glyphicon-chevron-down');
if(span){
$(".glyphicon-chevron-down").removeClass("glyphicon-chevron-down").addClass("glyphicon-chevron-right");
}
else{
$(".glyphicon-chevron-right").removeClass("glyphicon-chevron-right").addClass("glyphicon-chevron-down");
}
});
My table is declared in another .jsp file, but I do not think this code is needed for a solution. Also to mention, this loop is inside an AJAX call in my .js file.
Thanks!
I am trying to implement a view only HTML pane for some specific project. I am using JTextPane to render HTML using content type as "text/html". I was having tables in my input HTML so in order to give these tables border, i thought of using css styling, but unfortunately it did not work out.
If I put border property as part of table itself then it works but not
with css styling.
Here is the sample code i created to recreate issue. content1 does not creates border for my table but content2 creates it. I want to use content1 approach as i have plenty of html files with tables. Thanks for your time, any help will be much appreciated.
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
public class TestTextPane {
private static int X = 200;
private static int Y = 200;
private static int W = 600;
private static int H = 400;
public static final String content1 = "<html>\r\n" +
" <head>\r\n" +
" <style type=\"text/css\">\r\n" +
" <!--\r\n" +
" table,th, td { border: 1px solid black }\r\n" +
" body, p { font-family: Courier; font-size: 14 }\r\n" +
" -->\r\n" +
" </style>\r\n" +
" \r\n" +
" </head>\r\n" +
" <body>\r\n" +
" <div align=\"left\">\r\n" +
" <b>Q: What is the difference between GET and POST method? </b>\r\n" +
" </div>\r\n" +
" <p>\r\n" +
" A:\r\n" +
" </p>\r\n" +
" <table>\r\n" +
" <tr>\r\n" +
" <th width=\"50%\">\r\n" +
" GET\r\n" +
" </th>\r\n" +
" <th>\r\n" +
" POST\r\n" +
" </th>\r\n" +
" </tr>\r\n" +
" <tr>\r\n" +
" <td>\r\n" +
" GET is a safe method (idempotent)\r\n" +
" </td>\r\n" +
" <td>\r\n" +
" POST is non-idempotent method\r\n" +
" </td>\r\n" +
" </tr>\r\n" +
" <tr>\r\n" +
" <td>\r\n" +
" We can send limited data with GET method and it’s sent in the header \r\n" +
" request URL\r\n" +
" </td>\r\n" +
" <td>\r\n" +
" we can send large amount of data with POST because it’s part of the \r\n" +
" body.\r\n" +
" </td>\r\n" +
" </tr>\r\n" +
" </table>\r\n" +
" <br>\r\n" +
" <br>\r\n" +
" \r\n" +
" </body>\r\n" +
"</html>";
public static final String content2 = "<html>\r\n" +
" <head>\r\n" +
" <style type=\"text/css\">\r\n" +
" <!--\r\n" +
" body, p { font-family: Courier; font-size: 14 }\r\n" +
" -->\r\n" +
" </style>\r\n" +
" \r\n" +
" </head>\r\n" +
" <body>\r\n" +
" <div align=\"left\">\r\n" +
" <b>Q: What is the difference between GET and POST method? </b>\r\n" +
" </div>\r\n" +
" <p>\r\n" +
" A:\r\n" +
" </p>\r\n" +
" <table border=1>\r\n" +
" <tr>\r\n" +
" <th width=\"50%\">\r\n" +
" GET\r\n" +
" </th>\r\n" +
" <th>\r\n" +
" POST\r\n" +
" </th>\r\n" +
" </tr>\r\n" +
" <tr>\r\n" +
" <td>\r\n" +
" GET is a safe method (idempotent)\r\n" +
" </td>\r\n" +
" <td>\r\n" +
" POST is non-idempotent method\r\n" +
" </td>\r\n" +
" </tr>\r\n" +
" <tr>\r\n" +
" <td>\r\n" +
" We can send limited data with GET method and it’s sent in the header \r\n" +
" request URL\r\n" +
" </td>\r\n" +
" <td>\r\n" +
" we can send large amount of data with POST because it’s part of the \r\n" +
" body.\r\n" +
" </td>\r\n" +
" </tr>\r\n" +
" </table>\r\n" +
" <br>\r\n" +
" <br>\r\n" +
" \r\n" +
" </body>\r\n" +
"</html>";
/**
* #param args
*/
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setBounds(X, Y, W, H);
JTextPane pane = new JTextPane();
pane.setContentType("text/html");
pane.setEditable(false);
JScrollPane scrollPane = new JScrollPane(pane);
scrollPane.setBounds(X,Y,W,H);
frame.getContentPane().add(scrollPane);
pane.setText(content2); // change content here
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
I removed the comment notation from the style and added units for the size of the font.
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
public class TestTextPane {
private static int X = 200;
private static int Y = 200;
private static int W = 600;
private static int H = 400;
public static final String content1 = "<html>\r\n" +
" <head>\r\n" +
" <style type=\"text/css\">\r\n" +
" table, th, td { border: 2px solid #FF0000; }\r\n" +
// be sure to add a unit to the font size, otherwise it is invalid
" body, p { font-family: Courier; font-size: 14px; }\r\n" +
" </style>\r\n" +
" \r\n" +
" </head>\r\n" +
" <body>\r\n" +
" <div align=\"left\">\r\n" +
" <b>Q: What is the difference between GET and POST method? </b>\r\n" +
" </div>\r\n" +
" <p>\r\n" +
" A:\r\n" +
" </p>\r\n" +
" <table>\r\n" +
" <tr>\r\n" +
" <th width=\"50%\">\r\n" +
" GET\r\n" +
" </th>\r\n" +
" <th>\r\n" +
" POST\r\n" +
" </th>\r\n" +
" </tr>\r\n" +
" <tr>\r\n" +
" <td>\r\n" +
" GET is a safe method (idempotent)\r\n" +
" </td>\r\n" +
" <td>\r\n" +
" POST is non-idempotent method\r\n" +
" </td>\r\n" +
" </tr>\r\n" +
" <tr>\r\n" +
" <td>\r\n" +
" We can send limited data with GET method and it’s sent in the header \r\n" +
" request URL\r\n" +
" </td>\r\n" +
" <td>\r\n" +
" we can send large amount of data with POST because it’s part of the \r\n" +
" body.\r\n" +
" </td>\r\n" +
" </tr>\r\n" +
" </table>\r\n" +
" <br>\r\n" +
" <br>\r\n" +
" \r\n" +
" </body>\r\n" +
"</html>";
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setBounds(X, Y, W, H);
JTextPane pane = new JTextPane();
pane.setContentType("text/html");
pane.setEditable(false);
JScrollPane scrollPane = new JScrollPane(pane);
scrollPane.setBounds(X,Y,W,H);
frame.getContentPane().add(scrollPane);
pane.setText(content1); // change content here
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
This is what I tried for content1 and its working fine.
I added this line
body table { border: 1px solid red }
sample code:
public static final String content1 = "<html>\r\n"
+ " <head>\r\n"
+ " <style type=\"text/css\">\r\n"
+ " <!--\r\n"
+ " table, th, td { border: 1px solid black }\r\n"
+ " body table { border: 1px solid red }\r\n"
+ " body, p { font-family: Courier; font-size: 14; }\r\n"
+ " -->\r\n"
+ " </style>\r\n"
I tried all the above solutions but unfortunately any of these did not work for me. I was searching for the solution and i got into this post
And i tried table, th, td { border-width: 1px solid black } voila it worked for me. I dont know why the plain border property did not work for me but it worked for others.
Thanks a lot for all you help Guys.