2015-04-30 78 views
0

我有以下一段servlet代碼(在Apache Tomcat v7.0上運行)試圖從jsp頁面獲取用戶輸入,然後嘗試對其進行AES加密(在我的AES_BC使用充氣城堡)。我正在實例化我的AES_BC類,以便在方法myname()中進行測試。服務器啓動正常,我可以提供一些輸入,但是當在doGet中調用myname()時,我得到一個運行時異常。代碼和異常在下面給出:使用jsp servlet和充氣城堡運行時異常

import javax.servlet.ServletException; 
import javax.servlet.annotation.WebServlet; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 


import java.io.IOException; 
import java.io.PrintWriter; 
import java.text.SimpleDateFormat; 
import java.util.Date; 
import aes_bc.AES_BC; 

@WebServlet(name = "EncryptServlet", urlPatterns = {"/Encrypt"}) 
public class EncryptServlet extends HttpServlet { 
    private static final long serialVersionUID = 1L; 
    protected void doGet(HttpServletRequest request, 
     HttpServletResponse response) throws ServletException, IOException 
    { 
    // reading the user input 
    String query= request.getParameter("color"); 
    String color = "blue"; 
    PrintWriter out = response.getWriter(); 



    myname(); 
    System.out.println("color:"+color); 
    System.out.println("user query:"+query); 

    byte [] in_bytes = query.getBytes(); 

    String tsMsg = query + new SimpleDateFormat("yyyyMMddhhmm").format(new Date()); 
    System.out.println("time stamped message:"+tsMsg+"\n"); 

    out.println (
     "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"" + "http://www.w3.org/TR/html4/loose.dtd\">\n" + 
     "<html> \n" + 
     "<head> \n" + 
      "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"> \n" + 
      "<title> Bouncy Castle Encryption/Crypto JS decryption </title> \n" + 
     "</head> \n" + 
     "<body> \n" + 
      "<font size=\"9px\" color=\"" + color + "\">" + 
      "Plain text query:" + query + 
      "</font> \n" + 
      "<li>" + "Timestamped query:" + tsMsg + 
      "</li> \n" + 
     "</body> \n" + 
     "</html>" 
    ); 

    } 

    public void myname(){ 

     byte[] enc_key = "abcdefghijklmn".getBytes(); 
     AES_BC aesencrypt = new AES_BC(enc_key); 
     String msg1 = "test msg"; 
     byte [] in_bytes = msg1.getBytes(); 

     System.out.println("message:"+msg1); 
     } 
    } 

例外如下所示:

javax.servlet.ServletException: Servlet execution threw an exception 

root cause 

java.lang.NoClassDefFoundError: org/bouncycastle/crypto/BlockCipher 
    EncryptServlet.myname(EncryptServlet.java:59) 
    EncryptServlet.doGet(EncryptServlet.java:26) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:620) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:727) 

root cause 

java.lang.ClassNotFoundException: org.bouncycastle.crypto.BlockCipher 
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720) 
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571) 
    EncryptServlet.myname(EncryptServlet.java:59) 
    EncryptServlet.doGet(EncryptServlet.java:26) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:620) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:727) 

當我添加一個main方法將我AES_BC類和測試它獨立,它的作品精絕。我一直在摸索如何解決這個問題。任何幫助將不勝感激。

回答

3

你例外情況是很明顯的:

java.lang.ClassNotFoundException: org.bouncycastle.crypto.BlockCipher 

你還沒有得到必要的jar在類路徑中,如果你使用Maven,那麼添加一個依賴與compile範圍或以其它方式在互聯網上找到丟失的罐子,並把它添加到您的WEB-INF/lib文件夾。