Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import java libraries in jsp code?

Tags:

java

import

jsp

I have the following jsp code. I want to add libraries such as java.io.

How can I do this?

<% @page import=java.io.BufferedReader;
@page import=java.io.IOException;
@page import=java.io.InputStreamReader;
String IP=request.getParameter("IP");

String res="";

        Runtime run = Runtime.getRuntime();
        Process pr = run.exec("snmpget -v 2c -c public "+IP+" SNMPv2-MIB::sysUpTime.0");
        pr.waitFor();
        BufferedReader buf = new BufferedReader(new InputStreamReader(pr.getInputStream()));
        String line = "";
        //String res="";
            while ((line = buf.readLine()) != null)
            {
                res+=line+"\n";
            }
        int i=res.indexOf(")");
      //  System.out.println(i);

        res=res.substring(i+1).trim();

        //System.out.print(res);

    } catch (InterruptedException ex)
    {
        Logger.getLogger(myMain.class.getName()).log(Level.SEVERE, null, ex);
    }
    catch (IOException ex) {
            Logger.getLogger(myMain.class.getName()).log(Level.SEVERE, null, ex);
        }

%>
like image 844
py_script Avatar asked Mar 01 '11 19:03

py_script


People also ask

How do we import a package in JSP?

Full Stack Java developer - Java + JSP + Restful WS + SpringThe import attribute serves the same function as and behaves like, the Java import statement. The value for the import option is the name of the package you want to import. By default, a container automatically imports java. lang.

Can you import libraries in Java?

Built-in Packages The library is divided into packages and classes. Meaning you can either import a single class (along with its methods and attributes), or a whole package that contain all the classes that belong to the specified package.

Where do I put Java code in JSP?

JSP Scriptlet tag (Scripting elements) In JSP, java code can be written inside the jsp page using the scriptlet tag.

What is import in JSP?

1)import. The import attribute is used to import class,interface or all the members of a package.It is similar to import keyword in java class or interface.


1 Answers

You are almost right, but you need to close the import tag, like this:

<%@ page import="java.io.BufferedReader" %>

To declare multiple imports you can either duplicate that entire tag, like so:

<%@ page import="java.io.BufferedReader" %>
<%@ page import="java.io.InputStreamReader" %>

or use a comma-separated list:

<%@ page import="java.io.BufferedReader,java.io.InputStreamReader" %>

For a multitude of reasons, though, I would not recommend mixing java code into your JSPs.

like image 97
svjson Avatar answered Sep 30 '22 13:09

svjson