我正在尝试提交文本字段值并使用 servlet 打印它。index.jsp 是我的主页,我使用 jsp:include 来包含驻留在另一个页面中的表单,即 login.html。
这是我的 login.html 代码
<form id="f1" action="ControllerServlet" method="GET">
<p>username
<input class ="text-input" type="text" id="txtusername" />
</p>
<p>
<input type="submit" value="submit" />
</p>
Run Code Online (Sandbox Code Playgroud)
index.jsp
<div id="col3_content" class="clearfix">
<h1>H1 Heading</h1>
<jsp:include page="login.html"></jsp:include>
</div>
Run Code Online (Sandbox Code Playgroud)
控制器 servlet
String usrname = request.getParameter("txtusername").toString();
out.print(usrname);
Run Code Online (Sandbox Code Playgroud)
问题是这是抛出一个空指针异常。我在这里做错了什么?任何帮助表示赞赏。谢谢
如何doGet()从调用方法RequestDispatcher?
RequestDispatcher rd = sc.getRequestDispatcher("/CartServlet");
rd.forward(request, response);
此代码调用doPost()作为默认操作。
我正在提交带有文本和文件类型输入字段的表单,并使用此代码获取文本数据
但问题是
List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
for (FileItem item : items) {
if (item.isFormField()) {
// Process normal fields here.
//Taking all text and doing task
System.out.println("Field name: " + item.getFieldName());
System.out.println("Field value: " + item.getString());
} else {
// Process <input type="file"> here.
//And Leaving this at this time
}
}
Run Code Online (Sandbox Code Playgroud)
如果我解析请求并逐一迭代它,然后在formField中我用来获取所有文本参数,然后我再次在文件类型条件中使用此代码上传文件,因此它不会再次解析
List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
for (FileItem item : items) {
if (item.isFormField()) {
// Process normal fields here.
//Leaving this section this time …Run Code Online (Sandbox Code Playgroud) 我在将 java servlet 与 postgresql 连接时遇到了一些问题。如果可以,请帮助我
String dbName = "jdbc:postgresql://localhost/schedule_of_holidays";
String dbDriver = "org.postgresql.Driver";
Class.forName(dbDriver);
Connection con = DriverManager.getConnection(dbName, userName,
password);
System.out.println("Got Connection");
Statement statement = con.createStatement();
String sql = "select id from registration";
ResultSet rs = statement.executeQuery(sql);
while (rs.next()) {
System.out.println(rs.getInt("id"));
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试做一些非常简单的事情。我只想在 Spring MVC 应用程序中的 JSP 页面上显示一个列表。我在谷歌上搜索了很多,有很多例子(包括 Stackoverflow 上的一些),但它对我不起作用。我正在使用 Maven 和 Spring 3.2.4。这些是我的文件:
项目.java
public class Item
{
private final String value;
public Item(String value)
{
this.value = value;
}
public String getValue()
{
return value;
}
}
Run Code Online (Sandbox Code Playgroud)
主控制器.java
@Controller
public class MainController
{
@RequestMapping("/")
public String home(ModelMap map)
{
final List<Item> items = new ArrayList<Item>();
items.add(new Item("Value 1"));
items.add(new Item("Value 2"));
items.add(new Item("Value 3"));
items.add(new Item("Value 5"));
map.addAttribute(items);
return "list";
}
}
Run Code Online (Sandbox Code Playgroud)
列表.jsp
<!DOCTYPE html>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> …Run Code Online (Sandbox Code Playgroud) 我的jsp文件找不到我的javascript文件.在这里你可以看到我的jsp文件:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<script src="ressources/js/addMatrix.js"></script>
</head>
<body>
<div ng-app="App" ng-controller="MainCtrl">
<p>Add a new element : </p>
<table>
<tr>
<td data-ng-repeat="data in texts">
<button>{{data.text}}</button>
</td>
<td data-ng-repeat="field in fields">
<form ng-submit="submit(text)">
<input type="text" ng-model="text" name="text"
placeholder="New Category" />
</form>
</td>
<td>
<button ng-click="addNewChoice()">+</button>
</td>
</tr>
</table>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我的javascript文件位于文件夹/ WebContent/ressources/js中,jsp文件位于文件夹/ WebContent中.
我的servlet只在get方法中包含这个调用:
this.getServletContext().getRequestDispatcher("/index.jsp").forward(req, resp);
Run Code Online (Sandbox Code Playgroud)
我的web.xml文件如下所示:
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Home</servlet-name>
<servlet-class>com.pi.servlets.Index</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Home</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping> …Run Code Online (Sandbox Code Playgroud) 我正在为hello world创建一个简单的servlet来检查servlet是否正常工作,但在运行servlet并在运行servlet时映射web.xml文件后,它给了我一个错误.
我的代码:
请帮我解决我的问题??
I'm able to run application from my eclipse, but when i create jar try to run from command prompt it giving error. i'm using java 1.8 and eclipse kepler
@SpringBootApplication
public class Application {
public static void main(String[] args) {
ApplicationContext app = SpringApplication.run(Application.class, args);
Application application = app.getBean(Application.class);
if (args != null && args.length > 0) {
application.getAllApiByProject(args);
} else {
System.out.print("No Arguments found.., Pass Project argument");
}
final int exitCode = 0;
ExitCodeGenerator exitCodeGenerator = new ExitCodeGenerator() { …Run Code Online (Sandbox Code Playgroud) 我有一个基于HTTP servlet的web-app.这是对我的servlet的JS请求:
$.ajax({
type: "POST",
url: url,
xhrFields: {
withCredentials: false
},
data:{
"action" : action_type,
"fingerprint" : fingerprint,
"user_id" : user_id,
},
success: function(data) {
alert('sdp inviato!');
},
// vvv---- This is the new bit
error: function(jqXHR, textStatus, errorThrown) {
console.log("Error, status = " + textStatus + ", " +
"error thrown: " + errorThrown
);
}
});
Run Code Online (Sandbox Code Playgroud)
这是我的servlet代码:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String action= request.getParameter("action");
UtenteModel um= new UtenteModel();
SDPLineModel sdpmodel= new …Run Code Online (Sandbox Code Playgroud) 我试图检查以下所有条件是否返回true然后验证,
但是编译器没有说"运算符&&未定义参数类型boolean,String".我来自JS背景和一个dnot JAVA所以任何帮助将不胜感激.
String refererHeader = httprequest.getHeader("referer");
URL refererURL = new URL(refererHeader);
String requestUrl = httprequest.getRequestURL().toString();
String refererHost = refererURL.getHost();
int refererPort = refererURL.getPort();
String serverName = httprequest.getServerName();
int serverPort = httprequest.getServerPort();
if (refererHeader == null) {
httpresponse.setStatus(HttpServletResponse.SC_BAD_REQUEST);
return;
}
//Only if the refererhost , port match with server name and port and if the pathinfo matches
if(refererHeader.equals(requestUrl) &&
refererHost.equals(serverName) &&
serverPort == refererPort &&
refererURL.getPath() + "?" + refererURL.getQuery().equals(PATH)) {
chain.doFilter(request, response);
}
Run Code Online (Sandbox Code Playgroud) servlets ×10
java ×8
jsp ×4
el ×1
equals ×1
file-upload ×1
hibernate ×1
http ×1
jakarta-ee ×1
javascript ×1
jquery ×1
postgresql ×1
spring-boot ×1
spring-mvc ×1