Hallo,
ich habe im folgenden einen kleinen Servlet Beispiel, dieser kann aber wohl leider nur Strings versenden und empfangen. ich möchte aber mit einem java Objekt arbeiten. Hat da jemand vielleicht ein Tipp für mich?
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.HashMap;
import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class webServlet
*/
@WebServlet("/webServlet")
public class webServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
HashMap<String, String> map;
/**
* @see HttpServlet#HttpServlet()
*/
public webServlet() {
super();
// TODO Auto-generated constructor stub
}
@Override
public void init() throws ServletException {
map = new HashMap<String, String>();
map.put("3785724802",
"Titel:Inferno / Buchautor:Dan Brown / Preis: 26,00");
}
private void perform(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
try {
int length = request.getContentLength();
byte[] input = new byte[length];
ServletInputStream sin = request.getInputStream();
int c, count = 0;
while ((c = sin.read(input, count, input.length - count)) != -1) {
count += c;
}
sin.close();
String recievedString = new String(input);
System.out.println(recievedString);
for (String s : map.keySet()) {
if (s.equals(recievedString)) {
buchinfo = map.get(s);
break;
}
}
response.setStatus(HttpServletResponse.SC_OK);
OutputStreamWriter writer = new OutputStreamWriter(
response.getOutputStream());
writer.write(buchinfo);
writer.flush();
writer.close();
} catch (IOException e) {
try {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
response.getWriter().print(e.getMessage());
response.getWriter().close();
} catch (IOException ioe) {
}
}
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// perform(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
perform(request, response);
}
}```
Gruß JavaFan