我写了一个简单的servlet:
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class TestingServlet extends HttpServer {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("<H1>Hello world</H1>");
}
}
Run Code Online (Sandbox Code Playgroud)
我试着编译它并获得:
TestingServlet.java:6:错误:找不到符号公共类TestingServlet扩展HttpServer {^符号:类HttpServer 1错误
我该如何解决?
我做了一个有错误的特定项目:
发生问题:localhost上的服务器Tomcat v7.0服务器无法启动.
当我尝试启动tomcat来运行它.这是我的代码.这是从核心servlet书中获取的.
package com.sample;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
@WebServlet("/HelloServlet")
public class HelloServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public HelloServlet() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String docType =
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
"Transitional//EN\">\n";
out.println(docType +
"<HTML>\n" +
"<HEAD><TITLE>Hello</TITLE></HEAD>\n" +
"<BODY BGCOLOR=\"#FDF5E6\">\n" +
"<H1>Hello</H1>\n" +
"</BODY></HTML>");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException …Run Code Online (Sandbox Code Playgroud) 我已经在很多帖子中找到了这个问题,但是我找不到解决方案。我有一个基于领域的身份验证,并具有映射到USERS和ADMINS的两个角色。登录正常工作,但是如果我尝试使用
FacesContext context = FacesContext.getCurrentInstance();
ExternalContext externalContext = context.getExternalContext();
if(externalContext.getRemoteUser() != null && externalContext.isUserInRole("USERS")){
return true;
}
Run Code Online (Sandbox Code Playgroud)
即使我已登录,isUserInRole始终返回false。我该如何解决这个问题?
这是web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
<display-name>TravelDreamWeb</display-name>
<welcome-file-list>
<welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
<url-pattern>*.jsf</url-pattern>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<login-config>
<auth-method>FORM</auth-method>
<realm-name>authJdbcRealm</realm-name>
<form-login-config>
<form-login-page>/index.xhtml</form-login-page>
<form-error-page>/loginError.xhtml</form-error-page>
</form-login-config>
</login-config>
<security-constraint>
<web-resource-collection>
<web-resource-name>Admins Pages</web-resource-name>
<description />
<url-pattern>/admins/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>ADMINS</role-name>
</auth-constraint>
</security-constraint>
<security-constraint>
<web-resource-collection>
<web-resource-name>Users Pages</web-resource-name>
<description />
<url-pattern>/users/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>USERS</role-name>
</auth-constraint>
</security-constraint>
</web-app>
Run Code Online (Sandbox Code Playgroud)
这是glassfish-application.xml
<?xml …Run Code Online (Sandbox Code Playgroud) 我有一个关于servlet和jsp的问题.
Servlet的:
public class Servlet extends javax.servlet.http.HttpServlet {
protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
Integer i = new Integer(15);
request.setAttribute("var", i);
RequestDispatcher Dispatcher = getServletContext().getRequestDispatcher("/index.jsp");
Dispatcher.forward(request, response);
}
Run Code Online (Sandbox Code Playgroud)
JSP页面:
<html>
<head>
<title></title>
</head>
<body>
<form id="id" method="get" action="servlet">
<%= (request.getAttribute("var")) %>
</form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
结果我希望看到15,但我看到null.为什么会这样?
我有一个实现javax.servlet.Filter的类,在该过滤器中,我实例化了一个InterceptHttpRequestFilter和InterceptHttpResponseFilter的实例(用于修改传入和传出的请求和响应)
例:
public class InterceptHttpRequestFilter implements HttpServletRequest {
private HttpServletRequest httpReq;
final StringBuffer sb = new StringBuffer();
public InterceptHttpRequestFilter(ServletRequest request) {
this.httpReq = (HttpServletRequest) request;
try {
StringWriter sw = new StringWriter();
IOUtils.copy(request.getInputStream(), sw);
sb.append(sw.getBuffer().toString());
} catch (IOException e) {
e.printStackTrace();
}
}
....
Run Code Online (Sandbox Code Playgroud)
使用Servlet 2.5在Tomcat6上部署此项目时,一切正常.在Tomcat 7上部署它,我得到一个AbstractMethodError:
SEVERE: Servlet.service() for servlet [_______] in context with path [/__________-1.0.0] threw exception [Filter execution threw an exception] with root cause
java.lang.AbstractMethodError
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:225)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at com.mycee.project.filter.MyFilter.doFilter(MyFilter.java:182)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220) …Run Code Online (Sandbox Code Playgroud) <body>
<form method="POST" action="FileUpload" enctype="multipart/form-data" >
File:
<input type="file" name="fileSrc" > <br/>
<input type="submit" value="Upload" name="upload" >
</form>
</body>
Run Code Online (Sandbox Code Playgroud)
这是我的UploadImg.jsp,当我单击“上载”时,它会转到FileUpload.java,其中上载的图像必须存储在我指定的文件夹AppImages中,我该怎么做?谢谢你的帮助。
下面是我的Web应用程序的web.xml文件.我需要一个支持我的Web应用程序如何启动将逐步流程.
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<display-name>Tudu Lists</display-name>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>WEB-INF/log4j.xml</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:META-INF/spring/application-context.xml</param-value>
</context-param>
<!-- Define the basename for a resource bundle for I18N -->
<context-param>
<param-name>
javax.servlet.jsp.jstl.fmt.localizationContext
</param-name>
<param-value>messages</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<listener>
<listener-class>net.sf.navigator.menu.MenuContextListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/tudu/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>dwr</servlet-name>
<servlet-class>org.directwebremoting.spring.DwrSpringServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dwr</servlet-name>
<url-pattern>/ajax/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>rss</servlet-name>
<servlet-class>tudu.web.servlet.RssFeedServlet</servlet-class>
<load-on-startup>3</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>rss</servlet-name>
<url-pattern>/servlet/rss</url-pattern> …Run Code Online (Sandbox Code Playgroud) 如何使用此url打开index.jsp http://localhost:8080/myApp/,如何使用这样的超链接
<a href="/">HOME</a>转到index.jsp(http://localhost:8080/myApp/)?
这是我的web.xml:
<display-name>myApp</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/application-config.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>myApp</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>myApp</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Run Code Online (Sandbox Code Playgroud)
这是我的myApp-servlet.xml:
<context:component-scan base-package="org.myApp.com" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
Run Code Online (Sandbox Code Playgroud)
提前致谢!
我正试图通过ajax POST请求从我的jsp发送一个数组到我的Servlet.我的数组有一些包含许多字段的对象.如果我尝试发送带有11个对象的数组 - 使用JSON.stringify - 它工作正常(数组在服务器端接收),但是当我尝试发送一个包含12个以上对象的数组时会出现问题.错误是:400 Bad Request与谷歌浏览器调试器看,我能找到这个错误:fluxos:(unable to decode value)这里fluxos是我的数组的名字.
RELEVANTE代码部分:
for(var i=0; i<numberOfConnections; i++) {
fluxo = criaEstruturaFluxo(i);
fluxos.push(fluxo);
}
$.ajax({
type: "POST",
url: 'Servlet?fluxos='+JSON.stringify(fluxos),
success: function (data) {
alert('success');
}
});
...
function criaEstruturaFluxo(i) {
...
...
var fluxo = {
xOrigem: xOrigem,
yOrigem: yOrigem,
xDestino: xDestino,
yDestino: yDestino,
codWorkflow: codWorkflow,
acaoAvanco: acaoAvanco,
codAtividadeOrigem: codAtividadeOrigem[1],
codAtividadeDestino: codAtividadeDestino[1],
numero: numero,
nomeAtividadeOrigem: nomeAtividadeOrigem,
nomeAtividadeDestino: nomeAtividadeDestino,
codConexao: codConexao,
tipoOrigem: tipoOrigem,
tipoDestino: tipoDestino,
xFluxoOrigem: xFluxoOrigem, …Run Code Online (Sandbox Code Playgroud) 我的J2EE Web项目中的WEB-INF文件夹中有一个TLD(标记库描述符)文件.我从教科书中复制了TLD文件.在Eclipse EE中,在taglib行附近,它给出了错误:
根元素之前的文档中的标记必须格式正确.
XML/TLD文件:
<?xml version="1.0" encoding="ISO-8859-1" ?>
< taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd" version="2.0">
<tlib-version>1.2</tlib-version>
<uri>DiceFunctions</uri>
<function>
<name>rollIt</name>
<function-class>foo.DiceRoller</function-class>
<function-signature>
int rollDice()
</function-signature>
</function>
</taglib>
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个错误?
servlets ×10
java ×9
jsp ×5
java-ee ×2
spring ×2
spring-mvc ×2
tomcat ×2
ajax ×1
eclipse ×1
file-upload ×1
javascript ×1
jquery ×1
jsf ×1
maven ×1
xml ×1