无法解析为类型

发布于 2024-11-17 20:45:05 字数 1777 浏览 2 评论 0原文

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

    <%@ 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 %>

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 %>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

听你说爱我 2024-11-24 20:45:05

不要在 JSP 中定义内部类。将 JSP 视为一个简单的方法。

JSP 类似于(1):

public class MyJSP extends Servlet {
  public void service(HttpRequest request, HttpResponse response) {
     /** JSP CODE HERE **/
  }
}

定义内部类应该作为匿名内部类完成:

 Authenticator.setDefault(new Authenticator() {
   protected getPasswordAuthentication() {
     System.out.println("Requesting Host  : " + getRequestingHost());
     System.out.println("Requesting Port  : " + getRequestingPort());
     ...
   }
 });

不确定我会做什么来传递参数(我只使用了最简单的匿名内部类)。

无论如何,对于将从外部方法使用的任何内容,我将使用公共类(在其自己的文件中)并避免所有这些麻烦。

(1)不完全是这样,但你明白了。

Do not define inner classes in JSPs. Consider a JSP like a simple method.

A JSP is something like(1):

public class MyJSP extends Servlet {
  public void service(HttpRequest request, HttpResponse response) {
     /** JSP CODE HERE **/
  }
}

Defining an inner class should be done as an anonimous inner class:

 Authenticator.setDefault(new Authenticator() {
   protected getPasswordAuthentication() {
     System.out.println("Requesting Host  : " + getRequestingHost());
     System.out.println("Requesting Port  : " + getRequestingPort());
     ...
   }
 });

Not sure what would I do to pass parameters (I only have used the simplest anonymous inner classes).

Anyway for anything that is going to be used from outside methods, I would use a public class (in its own file) and avoid all these troubles.

(1)Not exactly this, but you get the idea.

橘和柠 2024-11-24 20:45:05

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

    <%@ 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 应该可以在您的代码中解析。

考虑将 Java 类移至单独的文件以使代码更具可读性。

<% ... %> 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 %>

Then MyAuthenticator should be resolvable in your code.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文