import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import java.io.IOException;
public class TestJackson {
Integer a;
public Integer getA() {
return a;
}
@JsonDeserialize(using = StringIntegerDeserializer.class)
public void setA(Integer a) {
this.a = a;
}
@Override
public String toString() {
return "TestJackson{a=" + a + "}";
}
public static void main(String[] args) throws IOException {
ObjectMapper mapper = new ObjectMapper();
String json = "{\"a\":\"5.666\"}";
System.out.println(mapper.readValue(json, TestJackson.class));
json = "{\"a\":5.666}";
System.out.println(mapper.readValue(json, TestJackson.class));
json = "{\"a\":5}";
System.out.println(mapper.readValue(json, TestJackson.class));
}
public static class StringIntegerDeserializer extends JsonDeserializer<Integer> {
@Override
public Integer deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
String valueAsString = jsonParser.getValueAsString();
int integer = (int) Double.parseDouble(valueAsString);
return integer;
}
}
}
<form ...>
<input name="lastName" value="Ivanov" />
<input name="firstName" value="Ivan" />
<input name="lastName" value="Petrov" />
<input name="firstName" value="Petr" />
</form>
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// тут будит lastNames = {"Ivanov", "Petrov"}
String[] lastNames = request.getParameterValues("lastName");
// тут будит firstNames = {"Ivan", "Petr"}
String[] firstNames = request.getParameterValues("firstName");
List<Author> author = new ArrayList<Author>();
for (int i = 0; i < lastNames.length && i < firstNames.length; i ++) {
author.add(new Author(firstNames[i], lastNames[i]));
}
}
@WebServlet(urlPatterns = "/home") // javax.servlet-api 3.0
public class HomeServlet extends HttpServlet {
private final Gson gson = new Gson();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
List<String> users = Arrays.asList("Vaya", "Petya", "Fedya");
String json = gson.toJson(users);
req.setAttribute("usersJson", json);
req.getRequestDispatcher("/home.jsp").forward(req,resp);
}
}
<html>
<head>
<script src="js/jquery-1.9.1.js"></script>
</head>
<body>
<script>
var users = ${usersJson};
$.each(users, function(i, val) {
.....................
});
</script>
</body>
</html>
@WebServlet(urlPatterns = "/json")
public class JsonServlet extends HttpServlet {
private final Gson gson = new Gson();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
List<String> users = Arrays.asList("Vaya", "Petya", "Fedya");
resp.addHeader("Content-Type", "application/json");
String json = gson.toJson(users);
resp.getWriter().print(json);
resp.flushBuffer();
}
}
<html>
<head>
<script src="js/jquery-1.9.1.js"></script>
</head>
<body>
<script>
$.get('json', function(data) {
$.each(data, function(i, val) {
..............................
});
});
</script>
</body>
</html>
@WebServlet(urlPatterns = "/") // javax.servlet-api 3.0
public class HomeServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
List<String> users = Arrays.asList("Vaya", "Petya", "Fedya");
req.setAttribute("users", users); // с помощью атрибутов передаются данные между сервлетами
req.getRequestDispatcher("/home.jsp").forward(req,resp);
}
}
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<body>
<h2>Hello World!</h2>
<c:forEach items="${users}" var="usr">
<p>${usr}</p>
</c:forEach>
</body>
</html>
[
{name: "pNode 01", children: [
{name: "child 01"},
{name: "child 02"}
]}
]
java Game>file.txt
java -jar Game.jar>file.txt
static {
try {
System.setOut(new PrintStream(new File("file.txt")));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws FileNotFoundException {
System.out.println("Hello World!"); // будет записано в файл
}
Thread t = new Thread.... {
if (Thread.interrupted()) {
// сохранить текущий результат и выйти из метода
}
});;
t.start();
t.join(10000); // ждем поток 10 сек
t.interrupt(); // предлагаем потоку прервать выполнение
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
package ru.toster.java.q261000;
import java.io.*;
import java.util.*;
public class MainQ261000 {
public static void main(String[] args) {
String demoFile = "aaaaaaaaa\nbbbbbbbbbbbb\nccccccccccccccccc";
Reader r = new StringReader(demoFile);
// Reader r = new FileReader("FileWithTextLines.txt");
List<String> lines = new ArrayList<String>();
Scanner s = new Scanner(r);
try {
while(s.hasNextLine()) {
lines.add(s.nextLine());
}
} finally {
s.close();
}
for (String line : lines) {
System.out.println(line);
}
}
}