无法解析为类型
我正在尝试在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要在 JSP 中定义内部类。将 JSP 视为一个简单的方法。
JSP 类似于(1):
定义内部类应该作为匿名内部类完成:
不确定我会做什么来传递参数(我只使用了最简单的匿名内部类)。
无论如何,对于将从外部方法使用的任何内容,我将使用公共类(在其自己的文件中)并避免所有这些麻烦。
(1)不完全是这样,但你明白了。
Do not define inner classes in JSPs. Consider a JSP like a simple method.
A JSP is something like(1):
Defining an inner class should be done as an anonimous inner class:
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.
<% ... %>
使 JSP 将代码内容视为语句。因此,您的类成为局部类,遵循与局部变量相同的作用域规则(即,您必须在使用该类之前声明该类)。我没有测试它,但如果您将代码重写为:那么
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:Then
MyAuthenticator
should be resolvable in your code.Consider moving the Java class to a separate file to make your code more readable.