顯示具有 servlet 標籤的文章。 顯示所有文章
顯示具有 servlet 標籤的文章。 顯示所有文章

2013年11月26日 星期二

onclick call servlet onclick-servlet onclick-window.open

html-onclick-http
$("#printCaption0").click(function(){
alert(123);
window.open("servlet","_blank","toolbar=0,scrollbars=0,location=0,location=no,width=1000,height=600")
});



html-onclick-servlet


<div id="userSetting" title="User Setting"  style="display:none;height:680px;width:80%;border:1px solid black;">
<form method="post"
action="#"
id="myform" class="focusContent" onsubmit="return validateForm()">
<p>
<label for="com" > Login ID  </label>
<input type="text" id="eMail" name="eMail" style="width:30%" disabled value="<% out.println(bean.getLoginid());%>" />
</p>
<p>
<label for="com" > Old Password </label>
<input type="password" id="oldPassword" name="oldPassword" style="width:21%"  />
</p>
<p>
<label for="com" > Password </label>
<input type="password" id="newPassword" name="newPassword" style="width:21%" />
</p>
<p>
<label for="com" > Confirm Password </label>
<input type="password" id="confirmPassword" name="confirmPassword" style="width:21%"/>
</p>
<p>
<input type="Submit" id="SubmitPassword" name="SubmitPassword" style="width:21%"/>
</p>
</form>
</div>
</body>
<script>

function validateForm()
{
s=_lookup("oldPassword");
s0='<%=bean.getPassword().trim()%>';
if (s != s0){alert("Old Password Not Match !!");return false;}
s1=_lookup("newPassword").trim();
s2=_lookup("confirmPassword").trim();
s3=<%=bean.getRecordid().trim()%>;
if (s1 != s2){alert("New Password Not Match Confirm Password!!");return false;}
/*20131127*/
s= "tableWrite?"+
"table=employee&"+
"fields=password&"+
"values='"+s1+"'"+
"&key=recordID&value="+s3+
"&callback=?";
$.getJSON(s,
function(data) {
if (data[0]){
}  else { 
}
}
); //endofgetJson
alert("Password Had Change")
}

2013年11月18日 星期一

javascript-servlet javascript-call-servlet




{onchange}
var is_banked=(this.checked?1:0);
var s160610="tableWrite?"+
"db="+DATABASE+"&"+
"table=PCARD_HEADER&"+
"fields=IS_BANKED&"+
"values='"+is_banked+"'"
+"&key=recordID&value="+Xdata[thisRowNo]+
"&callback=?";
$.getJSON(s160610);
{/onchange}

******************************************************


javascript-call-servlet


$.get('http://192.168.10.80:8080/hr/ServletName?Parameter1=64&Parameter2=2013&Parameter3=8', function(data) {});

s= "tableWrite?"+
"table=employee&"+
"fields=password&"+
"values='"+s1+"'"+
"&key=recordID&value="+s3+
"&callback=?";
$.getJSON(s,
function(data) {
if (data[0]){
}  else {
}
}
); //endofgetJson











2013年11月5日 星期二

servlet-download example (it can run)


inf : http://www.mkyong.com/servlet/servlet-code-to-download-text-file-from-website-java/

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServletDownloadDemo extends HttpServlet{

  private static final int BYTES_DOWNLOAD = 1024;

  public void doGet(HttpServletRequest request,
   HttpServletResponse response) throws IOException{
response.setContentType("text/plain");
response.setHeader("Content-Disposition",
                     "attachment;filename=downloadname.txt");
ServletContext ctx = getServletContext();
InputStream is = ctx.getResourceAsStream("/testing.txt");

int read=0;
byte[] bytes = new byte[BYTES_DOWNLOAD];
OutputStream os = response.getOutputStream();

while((read = is.read(bytes))!= -1){
os.write(bytes, 0, read);
}
os.flush();
os.close();
   }
}

servlet-download-file



inf : http://www.mkyong.com/servlet/servlet-code-to-download-text-file-from-website-java/


download file

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
public class ServletDownloadDemo extends HttpServlet{
 
  private static final int BYTES_DOWNLOAD = 1024;
 
  public void doGet(HttpServletRequest request, 
   HttpServletResponse response) throws IOException{
 response.setContentType("text/plain");
 response.setHeader("Content-Disposition",
                     "attachment;filename=downloadname.txt");
 ServletContext ctx = getServletContext();
 InputStream is = ctx.getResourceAsStream("/testing.txt");
 
 int read=0;
 byte[] bytes = new byte[BYTES_DOWNLOAD];
 OutputStream os = response.getOutputStream();
 
 while((read = is.read(bytes))!= -1){
  os.write(bytes, 0, read);
 }
 os.flush();
 os.close(); 
   }
}

servlet-template (simple)


import java.io.File;
import java.io.FileInputStream;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DNIR56B extends javax.servlet.http.HttpServlet {

static XMLIR56B XMLIR56B=new XMLIR56B();
static ZipFile zip=new ZipFile();

    public static void main(String[] args)  throws Exception  {
zip.go();
}

public DNIR56B(){ }
public void processRequest(HttpServletRequest request, HttpServletResponse response) {
try {
PrintWriter out = response.getWriter();
out.println(2);
XMLIR56B.go();
zip.go();
out.println(3);
}
catch (Exception e)
{
e.printStackTrace();
}
}

public void doGet(HttpServletRequest request, HttpServletResponse response){
processRequest(request, response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response){
doGet(request, response);
}

}

2013年10月24日 星期四

PDFServlet.java , servlet-pdf-read-save to client side

import java.io.File;
import java.io.FileInputStream;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class PDFServlet extends javax.servlet.http.HttpServlet{
static genIR56EPDF genIR56EPDF = new genIR56EPDF();
final static String CONTENT_TYPE = "application/pdf";
publicPDFServlet(){ }
public void doGet(HttpServletRequest request, HttpServletResponse response){
processRequest(request, response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response){
doGet(request, response);
}

public void processRequest(HttpServletRequest request, HttpServletResponse response){
response.setContentType(this.CONTENT_TYPE);
try{
genIR56EPDF.run();
File file1 = new File("/hr/payrollSlip.pdf");
FileInputStream fs = new FileInputStream(file1);
byte[] byteArray = new byte[(int) file1.length()];
fs.read(byteArray);
fs.close();
response.getOutputStream().write(byteArray);
}catch(Exception e){
this.outErrorMessage(response, "Some exception occurred: " + e.getMessage());
return;
}
}

private void outErrorMessage(HttpServletResponse response, String errorMsg){
response.setContentType("text/plain");
try{
PrintWriter out = response.getWriter();
out.println(errorMsg);
}catch(Exception e){ }
}
}

2013年9月9日 星期一

servlet-parameter

servlet parameter

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/javascript");
response.setCharacterEncoding("UTF-8");

table = request.getParameter("table");
fields = request.getParameter("fields");
format = request.getParameter("format");

2013年8月25日 星期日

servlet-trouble-shooting

inf : http://www.coderanch.com/how-to/java/ServletsFaq

ServletNotRunning404 : Why am I getting 404, the requested resource is not available?
RedirectForward : What is the difference between sendRedirect and forward?
ParametersAndAttributes : What is the difference between parameters and attributes?
Model1Model2MVC : What is MVC? What is the difference between Model 1 and Model 2?
CompilingServlets : How do I compile a servlet?
NoCacheHeaders : How do I prevent the browser from caching my dynamic content?
FileUpload : How do I upload a file in a servlet app?
IllegalStateException : The most common cause of this exception and an easy rule of thumb for preventing it.
ServletFilters : information, articles and useful filters
DeclaringListeners : Declaring lifecycle listeners in web.xml
InvokerServlet : how to access a servlet using its fully qualified class name instead of mapping it in web.xml (hint: it's a bad idea to do this)
TypesOfPaths: what are the various types of paths one might see or use in web applications?
RelativeLinks: How to build robust server-relative links in Servlet and JSP applications
SpecificationUrls : Where to find Servlet, JSP and HTTP specifications
What are the differences between the various Servlet API versions? (link)
What are some Java hosting companies? (link)
In the interface javax.servlet.RequestDispatcher, what is the difference between the forward(ServletRequest, ServletResponse) and include(ServletRequest, ServletResponse) methods? (link)
What is the difference between the init() and init(ServletConfig) methods? (link)
Why do servlets have an init method? Can't we make use of the servlet constructor for initialization? When should I call the destroy method? What happens if i call destroy() from init()? (link)
What is the difference between the getRequestDispatcher(String) and getNamedDispatcher(String) methods in the ServletContext Class? (link)
How can I filter the j_security_check URL used for FORM authentication? (link)
What is the difference between GenericServlet and HttpServlet? (link)
Why do we override the doGet and/or doPost methods instead of the service method? (link)
How can I use servlets with protocols other than HTTP, e.g. FTP? (link)
How do I implement security for my web application? (link)
Article on how to enable GZIP compression on Resin, Tomcat and Apache httpd
The 2nd edition of Volume 1 of Core Servlets and JSP is available as PDF for free.
Article on detecting the client browser and operating system version
Article on creating an ImageServlet to serve multimedia content from your servlet-based web application.
Article on creating thread-safe servlets.

    What are the differences between the various Servlet API versions? What's new in Servlet API 2.1: http://www.servlets.com/soapbox/servlet21.html
What's new in Servlet API 2.2: http://www.servlets.com/soapbox/servlet22.html
What's new in Servlet API 2.3: http://www.servlets.com/soapbox/servlet23.html
What's new in Servlet API 2.4: http://www.javaworld.com/jw-03-2003/jw-0328-servlet.html
What's new in Servlet API 2.5: Changelog Article
What's new in Servlet API 3.0: Article, async support

    What are some java hosting companies? This list is not an endorsement of any kind. It simply points to companies that have been mentioned by someone at some point. Feel free to add your own favorite. A list of Java PaaS providers can be found here.

2013年8月14日 星期三

java-servlet-implement-deployment

HTTP Status 404

-ROOT
----myapp
------index.html
------WEB-INF
---------classes
-----------javaName.java
-----------javaname.class

step 
1 make a file under /projectName/classes/javaName.java
2 compile : javac -classpath ./:/usr/share/apache-tomcat-7.0.35/lib/servlet-api.jar javaName.java
3 put it into /porjectName/WEB-INF/web.xml

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
    version="2.4">
    <display-name>Sand Box Test </display-name>
    <description> Sand Box Test </description>
<servlet>
 <servlet-name>javaName</servlet-name>
 <servlet-class>javaName</servlet-class>
 </servlet>
 <servlet-mapping>
   <servlet-name>javaName</servlet-name>
    <url-pattern>/javaName</url-pattern>
 </servlet-mapping>
</web-app>














java-servlet

package javax.servlet does not exist

find / -name servlet-api.jar
javac -classpath /usr/share/tomcat6/lib/servlet-api.jar tableRead.java

2013年8月2日 星期五

servlet java html jsp upload file example sample eg e.g.



source : http://www.javaworld.com.tw/jute/post/view?bid=38&id=182676

web.xml

<?xml version="1.0"?>
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet> 

<servlet>
<servlet-name>File Upload Servlet</servlet-name>
<servlet-class>catalog.view.util.FileUploadServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

  <!-- File Upload Servlet Mapping -->
<servlet-mapping>
<servlet-name>File Upload Servlet</servlet-name>
<url-pattern>*.file</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.faces</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>

faces-config.xml

<?xml version="1.0"?>
<!DOCTYPE faces-config PUBLIC "-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.0//EN" "http://java.sun.com/dtd/web-facesconfig_1_0.dtd">
<faces-config>
<navigation-rule>
<from-view-id>/uploadResult1.jsp</from-view-id>
<navigation-case>
<description>
</description>
<from-outcome>success</from-outcome>
<to-view-id>/index.jsp</to-view-id>
</navigation-case>
</navigation-rule>

</faces-config>

FileUploadServlet.java

package catalog.view.util;

import java.util.List;
import java.util.Iterator;
import java.io.File;
//
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.io.IOException;

import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;

public class FileUploadServlet extends HttpServlet{


  public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    this.doPost(req, res);
  }


  public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    //this.logger.debug("upload image");
  
   try {
    String temp=getServletContext().getRealPath("/")+"temp";
String loadpath=getServletContext().getRealPath("/")+"dir";
DiskFileUpload fu = new DiskFileUpload();
fu.setSizeMax(5*1024*1024);
fu.setSizeThreshold(4096);
fu.setRepositoryPath(temp);


List fileItems = fu.parseRequest(req);
Iterator iter = fileItems.iterator();

while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (!item.isFormField()) {
String name = item.getName();
name=name.substring(name.lastIndexOf("\\")+1);
// out.println(name);
long size = item.getSize();
if((name==null||name.equals("")) && size==0)
continue;
// out.println(item.getName()+" Size="+item.getSize()+"<br>");

File fNew= new File("C:\\"+name);

item.write(fNew);

}
}

    
    req.getRequestDispatcher("/uploadResult.jsp").forward(req, res);
    } catch (Exception e) {
      //this.logger.error("Could not upload file.", e);
      throw new ServletException("Could not upload file." + e.toString());
    }
  }
}

index.jsp

<%@ page contentType="text/html; charset=GBK" %>
<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core" %>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html" %>

<html>
  <head>
  <title>Page of upload</title>
<link rel="stylesheet" type="text/css" href="styles.css"/>
</head>
<body>
  <f:view>
    <form enctype="multipart/form-data" method="POST" action="upload.file">
<table align="center" class="box">
  <tr><td style="padding:20">
    <table>
    
    <tr>
      <td align="left">
        <h:outputText value="choose the file you want to upload" />
      </td>
    </tr>
    <tr>
      <td align="center">
                <input type="file" name="uploadFile"/>
              </td>
            </tr>
            <tr>
              <td align="center">
                <input type="submit" value="Upload"/>
              </td>
            </tr>
          </table>
        </td></tr>
      </table>
      </form>
    </f:view>
  </body>
</html>

upLoadResult.jsp

<%@ page contentType="text/html; charset=GBK" %>
<%@ taglib prefix="f" uri="http://java.sun.com/jsf/core" %>
<%@ taglib prefix="h" uri="http://java.sun.com/jsf/html" %>

<html>
  <head>
  <title>page of upload Success</title>
  <link rel="stylesheet" type="text/css" href="stylesheet.css"/>
</head>
<body>
  <f:view>
<table align="center" class="box">
  <tr><td style="padding:20">
    <table>
      <tr>
        <td>
                <h:outputText value="Success!"/>
                <h:outputText value="File Name:"/>
<h:outputText value="#{FileUploadServlet.fu.name}"/>
              </td>
            </tr>
            <tr>
              <td align="center">
                <h:form id="uploadImageResultForm">
                  <h:commandButton value="Back" action="success"/>
                </h:form>
              </td>
            </tr>
          </table>
        </td></tr>
      </table>
</f:view>
</body>
</html>