无法解析为一种类型

无法解析为一种类型

问题描述:

我正在尝试在我的JSP代码中使用HTTP身份验证.但我在MyAuthenticator上遇到错误,无法解析为一种类型.我在jsp页面中编写的代码的语法正确吗?任何建议将不胜感激..

I am trying to use HTTP Authentication in my JSP code. But I am getting error on MyAuthenticator cannot be resolved to a type. Is the sytax correct for the code that I have writtent in jsp page. Any suggestions will be appreciated..

    <%@ page language="java" import="java.net.Authenticator,java.net.PasswordAuthentication,java.io.BufferedReader,java.net.*,java.io.*" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%

String urlToQuery = request.getParameter("url");
System.out.println(" " +urlToQuery);
//URL url = new URL(urlToQuery);



//InputStream in = conn.getInputStream();

String urlString = "";
String username = "";
String password = "";
Authenticator.setDefault(new MyAuthenticator(username, password));
URL url = new URL(urlToQuery);
URLConnection conn = url.openConnection();
InputStream content = (InputStream) url.getContent();
BufferedReader in = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = in.readLine()) != null) {
  System.out.println(line);
}
System.out.println("Done.");

 class MyAuthenticator extends Authenticator {
    private String username, password;

    public MyAuthenticator(String user, String pass) {
      username = user;
      password = pass;
    }

    protected PasswordAuthentication getPasswordAuthentication() {
      System.out.println("Requesting Host  : " + getRequestingHost());
      System.out.println("Requesting Port  : " + getRequestingPort());
      System.out.println("Requesting Prompt : " + getRequestingPrompt());
      System.out.println("Requesting Protocol: " + getRequestingProtocol());
      System.out.println("Requesting Scheme : " + getRequestingScheme());
      System.out.println("Requesting Site  : " + getRequestingSite());
      return new PasswordAuthentication(username, password.toCharArray());
    }
  }



%>

<%=line %>

<% ... %>导致JSP将代码内容视为语句.因此,您的类将成为与本地变量遵循相同的作用域规则的本地类(也就是说,必须在使用该类之前声明该类).我没有测试它,但是如果您将代码重写为:

<% ... %> leads JSP to treat the code contents as statements. Therefore you class becomes a local class following the same scope rules as local variables (that is, you must declare the class earlier than using it). I didn't test it, but if you rewrite your code to:

    <%@ page language="java" import="java.net.Authenticator,java.net.PasswordAuthentication,java.io.BufferedReader,java.net.*,java.io.*" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%

class MyAuthenticator extends Authenticator {
    private String username, password;

    public MyAuthenticator(String user, String pass) {
      username = user;
      password = pass;
    }

    protected PasswordAuthentication getPasswordAuthentication() {
      System.out.println("Requesting Host  : " + getRequestingHost());
      System.out.println("Requesting Port  : " + getRequestingPort());
      System.out.println("Requesting Prompt : " + getRequestingPrompt());
      System.out.println("Requesting Protocol: " + getRequestingProtocol());
      System.out.println("Requesting Scheme : " + getRequestingScheme());
      System.out.println("Requesting Site  : " + getRequestingSite());
      return new PasswordAuthentication(username, password.toCharArray());
    }
  }

String urlToQuery = request.getParameter("url");
System.out.println(" " +urlToQuery);
//URL url = new URL(urlToQuery);



//InputStream in = conn.getInputStream();

String urlString = "";
String username = "";
String password = "";
Authenticator.setDefault(new MyAuthenticator(username, password));
URL url = new URL(urlToQuery);
URLConnection conn = url.openConnection();
InputStream content = (InputStream) url.getContent();
BufferedReader in = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = in.readLine()) != null) {
  System.out.println(line);
}
System.out.println("Done.");





%>

<%=line %>

然后MyAuthenticator应该在您的代码中可以解决.

Then MyAuthenticator should be resolvable in your code.

请考虑将Java类移到一个单独的文件中,以使您的代码更具可读性.

Consider moving the Java class to a separate file to make your code more readable.