Creación de una parte del programa del lado del servidor
Frecuentes
Visto 52 veces
0
So I have to create a serverside part of my program know, which is topic I know almost nothing about.
What I need is to send a file to server, which will then send it along. I may also need to get some minor data (couple of ints, a string) from the server.
The thing is that I don't know where to even begin. I tried googling a bit, but I got lost servlets, applets, ports, sockets and whatnots...
Knowing what I have to do, can you tell me which classes should I use? I'll figure out the rest myself...
2 Respuestas
1
You can make code available through a servlet, callable by a URL, and run it on a tomcat server.
You start out by making a servlet. Create a class that extends HttpServlet
.
To handle HTTP GET requests, override the doGet method:
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException
To handle HTTP POST requests, override the doPost method:
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException
You can make these methods execute logic, or retrieve data, and expose it as something like HTML
, XML
or JSON
.
To map the servlet to the url, you need a web.xml
archivo.
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 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">
<servlet>
<servlet-name>nameOfYourServlet</servlet-name>
<servlet-class>com.your.package.ServletImplementationClass</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>nameOfYourServlet</servlet-name>
<url-pattern>/api/my/servlet</url-pattern>
</servlet-mapping>
</web-app>
To deploy this to a tomcat server, you need your web.xml and compiled classes to live inside a WEB-INF folder. Zip the WEB-INF folder into an archive, change the extension of the archive to war, and drop it in your tomcat webapps folder.
I think this is about the quickest way to get server logic running, but your way of implementation depends on what you want to achieve, and what server you want to use etc.
It's probably worth your time looking into servlets a bit more. See este enlace.
Respondido el 22 de Septiembre de 13 a las 01:09
-1
Deberías usar ServerSocket
y Socket
and related classes. For more on this, check out the Tutorial de redes Java.
Respondido el 22 de Septiembre de 13 a las 01:09
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas java server-side or haz tu propia pregunta.